﻿Imports System.Data
Imports ADOEntityKYC
Imports ADOEntityAMLO101
Imports ADOEntityAMLO102
Imports ADOEntityAMLO103
Imports ADOEntityAMLO1059
Imports ADOEntity
Imports System.IO
Imports System.Web.Configuration

Partial Class AMLO_rptAmlo
    Inherits Web.UI.BasePage
    Dim dsAmlo As New DataSet
    Dim clsDataTable As clsLINQToDataTable
    Dim AMLO101Entity As AMLO101Entities
    Dim AMLO102Entity As AMLO102Entities
    Dim AMLO103Entity As AMLO103Entities
    Dim AMLO1059Entity As AMLO1059Entities
    Dim intBeforeItemIndex As Integer
    Dim clsSessionMenuRight As clsSessionMenuRight
    Dim clsFormatDate As clsDateFormat
    Dim clsMasterDropDown As clsMasterDropDown
    Dim sUserEntity As UserEntities

    Enum enmUseWork
        report = 1
        ExportXML = 2
        CompleteExportXML = 3
    End Enum
#Region "Even From "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        chkIsLogin()
        clsSessionMenuRight = Session("ClsMenuRight")

        btnSearch.Attributes.Add("onclick", "NoDoubleSubmit();")

        If (Not IsPostBack) Then

            resourceSet()
            loadDropDown()
            ' dsAmlo = New DataSet()

            setDefaultSearch()

            If (clsSessionMenuRight.getRoleGroup > 59) Then
                ddlBranch.Enabled = True
            Else
                ddlBranch.Enabled = False
            End If

            If (clsSessionMenuRight.getRoleGroup > 79) Then
                ddlDepartment.Enabled = True
            Else
                ddlDepartment.Enabled = False
            End If


        Else
            'NewObjAmlo()
            'dsAmlo = Session("ds")
            'grdAmlo.DataSource = dsAmlo.Tables("Amlo")
            'grdAmlo.DataBind()
        End If

    End Sub

    Private Sub NewObjAmlo()
        If (ddlRrptType.SelectedValue = "M51") Then
            AMLO101Entity = New AMLO101Entities
        ElseIf (ddlRrptType.SelectedValue = "M52") Then
            AMLO102Entity = New AMLO102Entities
        ElseIf (ddlRrptType.SelectedValue = "M53") Then
            AMLO103Entity = New AMLO103Entities
        ElseIf (ddlRrptType.SelectedValue = "M57") Then
            AMLO1059Entity = New AMLO1059Entities
        End If
    End Sub

    Private Sub loadDropDown()
        clsMasterDropDown = New clsMasterDropDown

        clsMasterDropDown.setDeptToDdlSearch(ddlDepartment, _
                                       strDeptCode:=clsSessionMenuRight.getDeptCode, _
                                       strRoleCode:=clsSessionMenuRight.getRoleGroup)
        clsMasterDropDown.setBranchDdlSearch(ddlBranch, strDeptCode:=ddlDepartment.SelectedValue, _
                                                   strBranchCode:=clsSessionMenuRight.getBranchCode, _
                                            strRoleCode:=clsSessionMenuRight.getRoleGroup, _
                                            strZoneCode:=clsSessionMenuRight.getZoneCode)
        clsMasterDropDown.setDdlTransType_AMLO1059(ddlTransType)

        ddlRrptType.Items.Clear()
        setCboAmlo()

    End Sub

    Private Sub setCboAmlo()
        sUserEntity = New UserEntities
        Dim qryamlo = From amloIsUser In sUserEntity.SMENURIGHT _
                                Join sProg In sUserEntity.SPROGRAM _
                                On amloIsUser.PROGRAMCODE Equals sProg.PROGRAMCODE
                   Where amloIsUser.USERRIGHT = clsSessionMenuRight.getUserRight And _
                         (amloIsUser.PROGRAMCODE = "M51" Or amloIsUser.PROGRAMCODE = "M52" Or amloIsUser.PROGRAMCODE = "M53" Or amloIsUser.PROGRAMCODE = "M57")
                    Select sProg.PROGRAMCODE, sProg.PROGRAMNAME

        ddlRrptType.DataSource = qryamlo.ToList
        ddlRrptType.DataTextField = "PROGRAMNAME"
        ddlRrptType.DataValueField = "PROGRAMCODE"
        ddlRrptType.DataBind()


    End Sub

    Private Sub ClearObj()
        If (ddlRrptType.SelectedValue = "M51") Then
            AMLO101Entity.Dispose()
            AMLO101Entity = Nothing
        ElseIf (ddlRrptType.SelectedValue = "M52") Then
            AMLO102Entity.Dispose()
            AMLO102Entity = Nothing
        ElseIf (ddlRrptType.SelectedValue = "M53") Then

        ElseIf (ddlRrptType.SelectedValue = "M57") Then
            AMLO1059Entity.Dispose()
            AMLO1059Entity = Nothing
        End If
    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        Session("ds") = dsAmlo
    End Sub
#End Region

#Region "SetForm"

    Sub resourceSet()
        Dim page As Label = CType(Master.FindControl("lblPage"), Label)
        page.Text = "สรุปรายงาน"
        Dim lbl As Label = CType(Master.FindControl("lblForm"), Label)
        lbl.Text = "สรุปรายงาน ปปง."
    End Sub
#End Region

#Region "Even Button"
    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        If (rdolTypeApp.SelectedValue = 1) Then
            IsDataBindReport()
        ElseIf (rdolTypeApp.SelectedValue = 2) Then
            IsDataBindExportXML()
        ElseIf (rdolTypeApp.SelectedValue = 3) Then
            IsDataBindCompleteExportXML()
        End If
        clearGvIndex()
    End Sub
#End Region

#Region " Function "

    Public Sub setDefaultSearch()
        clsFormatDate = New clsDateFormat
        txtDateStart.Text = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, dteDateIsChk:=Date.Now)
        txtDateEnd.Text = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, dteDateIsChk:=Date.Now)
        'txtYear.Text = clsFormatDate.GetYearLast(clsDateFormat.enmDateCultureInfo.Th).Substring(2, 2)

        ddlRrptType.SelectedIndex = 0
        ddlRrptType_SelectedIndexChanged(ddlRrptType, New EventArgs)
        rdolTypeDate.SelectedIndex = 0
        rdolSignType.SelectedIndex = 0
        rdoUserConfirm.SelectedIndex = 0
        rdolIsSend.SelectedIndex = 0
        If Not (clsSessionMenuRight.getRoleGroup < 80) Then
            'rdolTypeApp.Items.RemoveAt(1)
            'rdolTypeApp.Items.RemoveAt(1)
            rdolTypeApp.Visible = True
            rdolTypeApp.SelectedValue = 1
            enableBtn(1)
        Else
            rdolTypeApp.Visible = False
            rdolTypeApp.SelectedValue = 1
            enableBtn(1)
        End If


    End Sub

    Sub CreateControlMP() 'อ้าง control ใน masterPage
        Dim img As Image = CType(Master.FindControl("imgHead"), Image)
        img.ImageUrl = "~/img/amlo01_02.jpg"
    End Sub

    Private Sub IsDataBindReport()
        clsDataTable = New clsLINQToDataTable
        clsFormatDate = New clsDateFormat
        Dim UserRight As clsSessionMenuRight
        UserRight = Session("clsMenuRight")

        If dsAmlo.Tables.Contains("Amlo") Then
            dsAmlo.Tables.Clear()
        End If
        NewObjAmlo()

        If (ddlRrptType.SelectedValue = "M51") Then
            Dim qryAmlo = From tblAmlo In AMLO101Entity.AMLO101 Join
                               tblAmloDetail In AMLO101Entity.AMLODETAIL101 On tblAmlo.RPTSEQ Equals tblAmloDetail.RPTSEQ Join
                               tblTrantype In AMLO101Entity.MTSCTYPE1 On tblAmlo.TRANSTYPE Equals tblTrantype.TYPE_TSC_ID
                         Where tblAmloDetail.RPTIDX = 1 And tblAmlo.RPTTYPE = 1 And tblAmlo.ISCOMPLETE = 1 _
                         Order By tblAmlo.RPTSEQ Descending, tblAmlo.DEPTCODE Descending, tblAmlo.BRANCHCODE Descending
                         Select tblAmlo.RPTYEAR, tblAmlo.RPTREV, tblAmlo.ISSEND, tblAmlo.RPTNO, tblAmloDetail.FIRSTNAME,
                                tblAmloDetail.LASTNAME, tblAmlo.BRANCHCODE, tblAmlo.DEPTCODE, tblAmlo.ZONECODE, tblAmlo.TRANSDATE,
                                TRANSTYPE = If(tblAmlo.TRANSTYPE = "99", tblAmlo.TRANSTYPEOTHER, tblTrantype.TYPE_TSC_NAME),
                                tblAmlo.SIGNTYPE, tblAmlo.UPDATEBY, tblAmlo.SAVEDATE,
                                tblAmlo.RPTSEQ, tblAmlo.ISCOMPLETE, tblAmlo.ISDELETE, tblAmlo.ISCONFIRM, tblAmlo.CREATEBY
            'Select tblAmlo.RPTYEAR, tblAmlo.RPTREV, tblAmlo.ISSEND, tblAmlo.RPTNO, tblAmloDetail.FIRSTNAME, tblAmloDetail.LASTNAME, tblAmlo.BRANCHCODE, tblAmlo.DEPTCODE, tblAmlo.ZONECODE, tblAmlo.TRANSDATE, tblAmlo.TRANSTYPE, tblAmlo.TRANSTYPEOTHER, tblAmlo.SIGNTYPE, tblAmlo.UPDATEBY, tblAmlo.SAVEDATE, tblAmlo.RPTSEQ, tblAmlo.ISCOMPLETE, tblAmlo.ISDELETE, tblAmlo.ISCONFIRM, tblAmlo.CREATEBY


            If (UserRight.getRoleGroup > 79) Then 'admin ระบบขึ้นไป

            ElseIf (UserRight.getRoleGroup > 69) Then 'Admin ฝ่าย

            ElseIf (UserRight.getRoleGroup > 59) Then 'Admin เขต 
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup > 49) Then 'Admin สาขา
                ' qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup < 50) Then 'user ทั่วไป
                'qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode And tbl_Amlo.CREATEBY = UserRight.getUserName)
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.CREATEBY = UserRight.getUserName)
            End If

            If Not (ddlDepartment.SelectedValue = "0") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = ddlDepartment.SelectedValue)
            End If

            If Not (ddlBranch.SelectedValue = "0") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.BRANCHCODE = ddlBranch.SelectedValue)
            End If

            'If Not (txtYear.Text = "") Then
            '    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTYEAR.Value = CInt(txtYear.Text))
            'End If

            If Not (txtNo.Text = "") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTNO.Contains(txtNo.Text))
            End If

            If rdolTypeDate.SelectedValue = 1 Then
                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.TRANSDATE >= strStartDate And tbl_Amlo.TRANSDATE <= strEndDate)
                End If
            Else

                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SAVEDATE >= strStartDate And tbl_Amlo.SAVEDATE <= strEndDate)
                End If
            End If

            If Not (rdolSignType.SelectedValue = 0) Then
                If (rdolSignType.SelectedValue = 1) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 1)
                ElseIf (rdolSignType.SelectedValue = 2) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 0)
                End If

            End If

            If Not (rdolIsSend.SelectedValue = 0) Then
                If (rdolIsSend.SelectedValue = 1) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 1)
                ElseIf (rdolIsSend.SelectedValue = 2) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 0)
                End If

            End If

            dsAmlo.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
            If (qryAmlo.Count = 0) Then
                lblMsg.Visible = True
                lblTotal.Text = ""

            Else
                lblMsg.Visible = False
                lblTotal.Text = "จำนวนรายการทั้งหมด " & dsAmlo.Tables("Amlo").Rows.Count & " รายการ "
            End If




        ElseIf (ddlRrptType.SelectedValue = "M52") Then
            Dim qryAmlo = From tblAmlo In AMLO102Entity.AMLO102 Join
                          tblAmloDetail In AMLO102Entity.AMLODETAIL102 On tblAmlo.RPTSEQ Equals tblAmloDetail.RPTSEQ Join
                          tblTrantype In AMLO102Entity.MTSCTYPE2 On tblAmlo.TRANSTYPE Equals tblTrantype.TSCTYPEID
                         Where tblAmloDetail.RPTIDX = 1 And tblAmlo.RPTTYPE = 2 And tblAmlo.ISCOMPLETE = 1 _
                         Order By tblAmlo.RPTSEQ Descending _
                         Select tblAmlo.RPTYEAR, tblAmlo.RPTREV, tblAmlo.ISSEND, tblAmlo.RPTNO, tblAmloDetail.FIRSTNAME, tblAmloDetail.LASTNAME,
                                tblAmlo.BRANCHCODE, tblAmlo.DEPTCODE, tblAmlo.ZONECODE, tblAmlo.TRANSDATE,
                                TRANSTYPE = If(tblAmlo.TRANSTYPE = "9", tblAmlo.TRANSTYPEOTHER, tblTrantype.TSCTYPENAME),
                                tblAmlo.SIGNTYPE, tblAmlo.UPDATEBY, tblAmlo.SAVEDATE, tblAmlo.RPTSEQ,
                                tblAmlo.ISCOMPLETE, tblAmlo.ISDELETE, tblAmlo.ISCONFIRM, tblAmlo.CREATEBY

            If (UserRight.getRoleGroup > 79) Then 'admin ระบบขึ้นไป

            ElseIf (UserRight.getRoleGroup > 69) Then 'Admin ฝ่าย

            ElseIf (UserRight.getRoleGroup > 59) Then 'Admin เขต 
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup > 49) Then 'Admin สาขา
                ' qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup < 50) Then 'user ทั่วไป
                'qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode And tbl_Amlo.CREATEBY = UserRight.getUserName)
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.CREATEBY = UserRight.getUserName)
            End If

            If Not (ddlDepartment.SelectedValue = "0") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = ddlDepartment.SelectedValue)
            End If

            If Not (ddlBranch.SelectedValue = "0") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.BRANCHCODE = ddlBranch.SelectedValue)
            End If

            'If Not (txtYear.Text = "") Then
            '    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTYEAR.Value = CInt(txtYear.Text))
            'End If

            If Not (txtNo.Text = "") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTNO.Contains(txtNo.Text))
            End If

            If rdolTypeDate.SelectedValue = 1 Then
                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.TRANSDATE >= strStartDate And tbl_Amlo.TRANSDATE <= strEndDate)
                End If
            Else

                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SAVEDATE >= strStartDate And tbl_Amlo.SAVEDATE <= strEndDate)
                End If
            End If

            If Not (rdolSignType.SelectedValue = 0) Then
                If (rdolSignType.SelectedValue = 1) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 1)
                ElseIf (rdolSignType.SelectedValue = 2) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 0)
                End If

            End If

            If Not (rdolIsSend.SelectedValue = 0) Then
                If (rdolIsSend.SelectedValue = 1) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 1)
                ElseIf (rdolIsSend.SelectedValue = 2) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 0)
                End If

            End If

            dsAmlo.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
            If (qryAmlo.Count = 0) Then
                lblMsg.Visible = True
                lblTotal.Text = ""

            Else
                lblMsg.Visible = False
                lblTotal.Text = "จำนวนรายการทั้งหมด " & dsAmlo.Tables("Amlo").Rows.Count & " รายการ "
            End If



        ElseIf (ddlRrptType.SelectedValue = "M53") Then
            Dim qryAmlo = From tblAmlo In AMLO103Entity.AMLO103 Join
                          tblAmloDetail In AMLO103Entity.AMLODETAIL103 On tblAmlo.RPTSEQ Equals tblAmloDetail.RPTSEQ Join
                          tblTrantype In AMLO103Entity.MTSCTYPE3 On tblAmlo.TRANSTYPE Equals tblTrantype.TSCTYPEID
                        Where tblAmloDetail.RPTIDX = 1 And tblAmlo.RPTTYPE = 3 And tblAmlo.ISCOMPLETE = 1 _
                        Order By tblAmlo.RPTSEQ Descending _
                        Select tblAmlo.RPTYEAR, tblAmlo.RPTREV, tblAmlo.ISSEND, tblAmlo.RPTNO, tblAmloDetail.FIRSTNAME, tblAmloDetail.LASTNAME,
                                tblAmlo.BRANCHCODE, tblAmlo.DEPTCODE, tblAmlo.ZONECODE, tblAmlo.TRANSDATE,
                                TRANSTYPE = tblTrantype.TSCTYPENAME,
                                tblAmlo.TRANSTYPEOTHER, signtype = If(tblAmlo.SIGNTYPE Is Nothing, 0, tblAmlo.SIGNTYPE), tblAmlo.UPDATEBY,
                                tblAmlo.SAVEDATE, tblAmlo.RPTSEQ, tblAmlo.ISCOMPLETE, tblAmlo.ISDELETE, tblAmlo.ISCONFIRM, tblAmlo.CREATEBY

            If (UserRight.getRoleGroup > 79) Then 'admin ระบบขึ้นไป

            ElseIf (UserRight.getRoleGroup > 69) Then 'Admin ฝ่าย

            ElseIf (UserRight.getRoleGroup > 59) Then 'Admin เขต 
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup > 49) Then 'Admin สาขา
                'qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup < 50) Then 'user ทั่วไป
                'qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode And tbl_Amlo.CREATEBY = UserRight.getUserName)
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.CREATEBY = UserRight.getUserName)

            End If

            If Not (ddlDepartment.SelectedValue = "0") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = ddlDepartment.SelectedValue)
            End If

            If Not (ddlBranch.SelectedValue = "0") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.BRANCHCODE = ddlBranch.SelectedValue)
            End If

            'If Not (txtYear.Text = "") Then
            '    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTYEAR.Value = CInt(txtYear.Text))
            'End If

            If Not (txtNo.Text = "") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTNO.Contains(txtNo.Text))
            End If

            If rdolTypeDate.SelectedValue = 1 Then
                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.TRANSDATE >= strStartDate And tbl_Amlo.TRANSDATE <= strEndDate)
                End If
            Else

                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SAVEDATE >= strStartDate And tbl_Amlo.SAVEDATE <= strEndDate)
                End If
            End If



            If Not (rdolIsSend.SelectedValue = 0) Then
                If (rdolIsSend.SelectedValue = 1) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 1)
                ElseIf (rdolIsSend.SelectedValue = 2) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 0)
                End If

            End If

            dsAmlo.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
            If (qryAmlo.Count = 0) Then
                lblMsg.Visible = True
                lblTotal.Text = ""

            Else
                lblMsg.Visible = False
                lblTotal.Text = "จำนวนรายการทั้งหมด " & dsAmlo.Tables("Amlo").Rows.Count & " รายการ "
            End If


        ElseIf (ddlRrptType.SelectedValue = "M57") Then
            'Dim qryAmlo = From tblAmlo In AMLO1059Entity.AMLO1059 Join tblAmloDetail In AMLO103Entity.AMLODETAIL103 On tblAmlo.RPTSEQ Equals tblAmloDetail.RPTSEQ
            '           Where tblAmloDetail.RPTIDX = 1 And tblAmlo.RPTTYPE = 3 And tblAmlo.ISCOMPLETE = 1 _
            '           Order By tblAmlo.RPTSEQ Descending _
            '           Select tblAmlo.RPTYEAR, tblAmlo.RPTREV, tblAmlo.ISSEND, tblAmlo.RPTNO, tblAmloDetail.FIRSTNAME, tblAmloDetail.LASTNAME, tblAmlo.BRANCHCODE, tblAmlo.DEPTCODE, tblAmlo.ZONECODE, tblAmlo.TRANSDATE, tblAmlo.TRANSTYPE, signtype = If(tblAmlo.SIGNTYPE Is Nothing, 0, tblAmlo.SIGNTYPE), tblAmlo.UPDATEBY, tblAmlo.SAVEDATE, tblAmlo.RPTSEQ, tblAmlo.ISCOMPLETE, tblAmlo.ISDELETE, tblAmlo.ISCONFIRM, tblAmlo.CREATEBY
            Dim qryAmlo = From tblAmlo In AMLO1059Entity.AMLO1059 Join tblBranch In AMLO1059Entity.SBRANCHes _
                      On tblAmlo.INIT_SOL_ID Equals tblBranch.SOL_ID _
                     Group Join tblTran In AMLO1059Entity.MTSCTYPE_1_05_9 On tblAmlo.KEY_FLG Equals tblTran.TSCTYPEID
                      Into leftJoinAmlo = Group
                      From tblLeftAmlo In leftJoinAmlo.DefaultIfEmpty
                     Order By tblAmlo.ID Ascending _
                   Select tblAmlo.ID, tblAmlo.KEY_FLG, tblAmlo.P3_TRAN_ID, tblAmlo.P3_TRANS_TYPE, tblAmlo.P3_TRANS_DATE, tblAmlo.P3_TRAN_AMT, tblAmlo.INIT_SOL_ID, tblAmlo.P3_FORACID, tblAmlo.P3_CUST_NAME, tblAmlo.P2_CUST_FNAME, tblAmlo.P2_CUST_LNAME, _
                   tblAmlo.P3_FORACID2, tblAmlo.P3_CUST_NAME2, tblAmlo.P2_CUST_FNAME2, tblAmlo.P2_CUST_LNAME2, TSCTYPEID = If(tblLeftAmlo.TSCTYPEID Is Nothing, "-", tblLeftAmlo.TSCTYPEID), _
                   tblBranch.DEPTCODE, tblBranch.ZONECODE, tblBranch.BRANCHCODE, _
                   tblAmlo.P3_TRAN_CODE, tblLeftAmlo.TSCTYPENAME, tblAmlo.CREATEBY, tblAmlo.CREATEDATE, tblAmlo.UPDATEBY, tblAmlo.UPDATEDATE, _
                   tblAmlo.ISCONFIRM, tblAmlo.ISSEND, tblAmlo.ISDELETE, tblAmlo.DCN_DOCUMENTNO, tblAmlo.RPTREV

            If (UserRight.getRoleGroup > 79) Then 'admin ระบบขึ้นไป

            ElseIf (UserRight.getRoleGroup > 69) Then 'Admin ฝ่าย

            ElseIf (UserRight.getRoleGroup > 59) Then 'Admin เขต 
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup > 49) Then 'Admin สาขา
                'qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup < 50) Then 'user ทั่วไป
                'qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode And tbl_Amlo.CREATEBY = UserRight.getUserName)
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.CREATEBY = UserRight.getUserName)

            End If

            If Not (ddlDepartment.SelectedValue = "0") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = ddlDepartment.SelectedValue)
            End If

            If Not (ddlBranch.SelectedValue = "0") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.BRANCHCODE = ddlBranch.SelectedValue)
            End If

            'If Not (txtYear.Text = "") Then
            '    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTYEAR.Value = CInt(txtYear.Text))
            'End If

            If Not (txtNo.Text = "") Then
                'qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTNO.Contains(txtNo.Text))
            End If

            If rdolTypeDate.SelectedValue = 1 Then
                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text) & " 00:00:00"
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text) & " 23:59:59"
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.P3_TRANS_DATE >= strStartDate And tbl_Amlo.P3_TRANS_DATE <= strEndDate)
                End If
            Else

                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text) & " 00:00:00"
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text) & " 23:59:59"
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.CREATEDATE >= strStartDate And tbl_Amlo.CREATEDATE <= strEndDate)
                End If
            End If

            If Not (rdoUserConfirm.SelectedValue = -1) Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISCONFIRM = rdoUserConfirm.SelectedValue)
            End If

            If Not (rdolIsSend.SelectedValue = 0) Then
                If (rdolIsSend.SelectedValue = 1) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 1)
                ElseIf (rdolIsSend.SelectedValue = 2) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 0)
                End If

            End If

            dsAmlo.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
            If (qryAmlo.Count = 0) Then
                lblMsg.Visible = True
                lblTotal.Text = ""

            Else
                lblMsg.Visible = False
                lblTotal.Text = "จำนวนรายการทั้งหมด " & dsAmlo.Tables("Amlo").Rows.Count & " รายการ "
            End If
        End If

        If (ddlRrptType.SelectedValue = "M57") Then
            grdAmlo1059.Columns(13).Visible = False
            grdAmlo1059.Columns(14).Visible = False
            grdAmlo1059.DataSource = dsAmlo.Tables("Amlo")
            grdAmlo1059.DataBind()
        Else
            grdAmlo.Columns(9).Visible = False
            grdAmlo.Columns(10).Visible = False
            grdAmlo.DataSource = dsAmlo.Tables("Amlo")
            grdAmlo.DataBind()
        End If




        ClearObj()
    End Sub

    Private Sub IsDataBindExportXML()
        clsDataTable = New clsLINQToDataTable
        clsFormatDate = New clsDateFormat
        Dim UserRight As clsSessionMenuRight
        UserRight = Session("clsMenuRight")

        If dsAmlo.Tables.Contains("Amlo") Then
            dsAmlo.Tables.Clear()
        End If
        NewObjAmlo()

        If (ddlRrptType.SelectedValue = "M51") Then
            Dim qryAmlo = From tblAmlo In AMLO101Entity.AMLO101 Join tblAmloDetail In AMLO101Entity.AMLODETAIL101 On tblAmlo.RPTSEQ Equals tblAmloDetail.RPTSEQ
                         Where tblAmloDetail.RPTIDX = 1 And tblAmlo.ISCOMPLETE = 1 _
                         Order By tblAmlo.RPTSEQ Descending, tblAmlo.DEPTCODE Descending, tblAmlo.BRANCHCODE Descending _
                         Select tblAmlo.RPTYEAR, tblAmlo.RPTREV, tblAmlo.ISSEND, tblAmlo.RPTNO, tblAmloDetail.FIRSTNAME, tblAmloDetail.LASTNAME, tblAmlo.BRANCHCODE, tblAmlo.DEPTCODE, tblAmlo.ZONECODE, tblAmlo.TRANSDATE, tblAmlo.TRANSTYPE, tblAmlo.SIGNTYPE, tblAmlo.UPDATEBY, tblAmlo.SAVEDATE, tblAmlo.RPTSEQ, tblAmlo.ISCOMPLETE, tblAmlo.ISDELETE, tblAmlo.ISCONFIRM, tblAmlo.CREATEBY


            If (UserRight.getRoleGroup > 79) Then 'admin ระบบขึ้นไป

            ElseIf (UserRight.getRoleGroup > 69) Then 'Admin ฝ่าย

                ' qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = UserRight.getDeptCode)
            ElseIf (UserRight.getRoleGroup > 59) Then 'Admin เขต 
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup > 49) Then 'Admin สาขา
                ' qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup < 50) Then 'user ทั่วไป
                'qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode And tbl_Amlo.CREATEBY = UserRight.getUserName)
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.CREATEBY = UserRight.getUserName)
            End If

            If Not (ddlDepartment.SelectedValue = "0") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = ddlDepartment.SelectedValue)
            End If

            If Not (ddlBranch.SelectedIndex = 0) Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.BRANCHCODE.Contains(ddlBranch.SelectedValue))
            End If

            'If Not (txtYear.Text = "") Then
            '    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTYEAR.Value = CInt(txtYear.Text))
            'End If
            If Not (txtNo.Text = "") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTNO.Contains(txtNo.Text))
            End If

            If rdolTypeDate.SelectedValue = 1 Then
                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.TRANSDATE >= strStartDate And tbl_Amlo.TRANSDATE <= strEndDate)
                End If
            Else

                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SAVEDATE >= strStartDate And tbl_Amlo.SAVEDATE <= strEndDate)
                End If
            End If

            If Not (rdolSignType.SelectedValue = 0) Then
                If (rdolSignType.SelectedValue = 1) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 1)
                ElseIf (rdolSignType.SelectedValue = 2) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 0)
                End If

            End If

            If Not (rdolIsSend.SelectedValue = 0) Then
                If (rdolIsSend.SelectedValue = 1) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 1)
                ElseIf (rdolIsSend.SelectedValue = 2) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 0)
                End If

            End If

            dsAmlo.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
            If (qryAmlo.Count = 0) Then
                lblMsg.Visible = True
                lblTotal.Text = ""

            Else
                lblMsg.Visible = False
                lblTotal.Text = "จำนวนรายการทั้งหมด " & dsAmlo.Tables("Amlo").Rows.Count & " รายการ "
            End If


            'grdAmlo.DataSource = dsAmlo.Tables("Amlo")
            'grdAmlo.DataBind()
            'grdAmlo.Columns(9).Visible = False
            'grdAmlo.Columns(10).Visible = False

        ElseIf (ddlRrptType.SelectedValue = "M52") Then
            Dim qryAmlo = From tblAmlo In AMLO102Entity.AMLO102 Join tblAmloDetail In AMLO102Entity.AMLODETAIL102 On tblAmlo.RPTSEQ Equals tblAmloDetail.RPTSEQ
                         Where tblAmloDetail.RPTIDX = 1 _
                         Order By tblAmlo.RPTSEQ Descending _
                         Select tblAmlo.RPTYEAR, tblAmlo.RPTREV, tblAmlo.ISSEND, tblAmlo.RPTNO, tblAmloDetail.FIRSTNAME, tblAmloDetail.LASTNAME, tblAmlo.BRANCHCODE, tblAmlo.DEPTCODE, tblAmlo.ZONECODE, tblAmlo.TRANSDATE, tblAmlo.TRANSTYPE, tblAmlo.SIGNTYPE, tblAmlo.UPDATEBY, tblAmlo.SAVEDATE, tblAmlo.RPTSEQ, tblAmlo.ISCOMPLETE, tblAmlo.ISDELETE, tblAmlo.ISCONFIRM, tblAmlo.CREATEBY

            If (UserRight.getRoleGroup > 79) Then 'admin ระบบขึ้นไป
            ElseIf (UserRight.getRoleGroup > 69) Then 'Admin ฝ่าย

                'qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = UserRight.getDeptCode)
            ElseIf (UserRight.getRoleGroup > 59) Then 'Admin เขต 
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup > 49) Then 'Admin สาขา
                'qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup < 50) Then 'user ทั่วไป
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.CREATEBY = UserRight.getUserName)

            End If

            If Not (ddlDepartment.SelectedValue = "0") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = ddlDepartment.SelectedValue)
            End If


            If Not (ddlBranch.SelectedIndex = 0) Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.BRANCHCODE.Contains(ddlBranch.SelectedValue))
            End If

            'If Not (txtYear.Text = "") Then
            '    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTYEAR.Value = CInt(txtYear.Text))
            'End If
            If Not (txtNo.Text = "") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTNO.Contains(txtNo.Text))
            End If

            If rdolTypeDate.SelectedValue = 1 Then
                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.TRANSDATE >= strStartDate And tbl_Amlo.TRANSDATE <= strEndDate)
                End If
            Else

                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SAVEDATE >= strStartDate And tbl_Amlo.SAVEDATE <= strEndDate)
                End If
            End If

            If Not (rdolSignType.SelectedValue = 0) Then
                If (rdolSignType.SelectedValue = 1) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 1)
                ElseIf (rdolSignType.SelectedValue = 2) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 0)
                End If

            End If

            If Not (rdolIsSend.SelectedValue = 0) Then
                If (rdolIsSend.SelectedValue = 1) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 1)
                ElseIf (rdolIsSend.SelectedValue = 2) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 0)
                End If

            End If

            dsAmlo.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
            If (qryAmlo.Count = 0) Then
                lblMsg.Visible = True
                lblTotal.Text = ""

            Else
                lblMsg.Visible = False
                lblTotal.Text = "จำนวนรายการทั้งหมด " & dsAmlo.Tables("Amlo").Rows.Count & " รายการ "
            End If


        ElseIf (ddlRrptType.SelectedValue = "M53") Then
            Dim qryAmlo = From tblAmlo In AMLO103Entity.AMLO103 Join tblAmloDetail In AMLO103Entity.AMLODETAIL103 On tblAmlo.RPTSEQ Equals tblAmloDetail.RPTSEQ
                       Where tblAmloDetail.RPTIDX = 1 And tblAmlo.RPTTYPE = 3 And tblAmlo.ISCOMPLETE = 1 _
                       Order By tblAmlo.RPTSEQ Descending _
                       Select tblAmlo.RPTYEAR, tblAmlo.RPTREV, tblAmlo.ISSEND, tblAmlo.RPTNO, tblAmloDetail.FIRSTNAME, tblAmloDetail.LASTNAME, tblAmlo.BRANCHCODE, tblAmlo.DEPTCODE, tblAmlo.ZONECODE, tblAmlo.TRANSDATE, tblAmlo.TRANSTYPE, signtype = If(tblAmlo.SIGNTYPE Is Nothing, 0, tblAmlo.SIGNTYPE), tblAmlo.UPDATEBY, tblAmlo.SAVEDATE, tblAmlo.RPTSEQ, tblAmlo.ISCOMPLETE, tblAmlo.ISDELETE, tblAmlo.ISCONFIRM, tblAmlo.CREATEBY

            If (UserRight.getRoleGroup > 79) Then 'admin ระบบขึ้นไป

            ElseIf (UserRight.getRoleGroup > 69) Then 'Admin ฝ่าย

            ElseIf (UserRight.getRoleGroup > 59) Then 'Admin เขต 
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup > 49) Then 'Admin สาขา
                'qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup < 50) Then 'user ทั่วไป
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.CREATEBY = UserRight.getUserName)

            End If

            If Not (ddlDepartment.SelectedValue = "0") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = ddlDepartment.SelectedValue)
            End If

            If Not (ddlBranch.SelectedValue = "0") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.BRANCHCODE = ddlBranch.SelectedValue)
            End If

            'If Not (txtYear.Text = "") Then
            '    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTYEAR.Value = CInt(txtYear.Text))
            'End If

            If Not (txtNo.Text = "") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTNO.Contains(txtNo.Text))
            End If

            If rdolTypeDate.SelectedValue = 1 Then
                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.TRANSDATE >= strStartDate And tbl_Amlo.TRANSDATE <= strEndDate)
                End If
            Else

                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SAVEDATE >= strStartDate And tbl_Amlo.SAVEDATE <= strEndDate)
                End If
            End If



            If Not (rdolIsSend.SelectedValue = 0) Then
                If (rdolIsSend.SelectedValue = 1) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 1)
                ElseIf (rdolIsSend.SelectedValue = 2) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 0)
                End If

            End If

            dsAmlo.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
            If (qryAmlo.Count = 0) Then
                lblMsg.Visible = True
                lblTotal.Text = ""

            Else
                lblMsg.Visible = False
                lblTotal.Text = "จำนวนรายการทั้งหมด " & dsAmlo.Tables("Amlo").Rows.Count & " รายการ "
            End If

        ElseIf (ddlRrptType.SelectedValue = "M57") Then
            'Dim qryAmlo = From tblAmlo In AMLO1059Entity.AMLO1059 Join tblAmloDetail In AMLO103Entity.AMLODETAIL103 On tblAmlo.RPTSEQ Equals tblAmloDetail.RPTSEQ
            '           Where tblAmloDetail.RPTIDX = 1 And tblAmlo.RPTTYPE = 3 And tblAmlo.ISCOMPLETE = 1 _
            '           Order By tblAmlo.RPTSEQ Descending _
            '           Select tblAmlo.RPTYEAR, tblAmlo.RPTREV, tblAmlo.ISSEND, tblAmlo.RPTNO, tblAmloDetail.FIRSTNAME, tblAmloDetail.LASTNAME, tblAmlo.BRANCHCODE, tblAmlo.DEPTCODE, tblAmlo.ZONECODE, tblAmlo.TRANSDATE, tblAmlo.TRANSTYPE, signtype = If(tblAmlo.SIGNTYPE Is Nothing, 0, tblAmlo.SIGNTYPE), tblAmlo.UPDATEBY, tblAmlo.SAVEDATE, tblAmlo.RPTSEQ, tblAmlo.ISCOMPLETE, tblAmlo.ISDELETE, tblAmlo.ISCONFIRM, tblAmlo.CREATEBY
            Dim qryAmlo = From tblAmlo In AMLO1059Entity.AMLO1059 Join tblBranch In AMLO1059Entity.SBRANCHes _
                      On tblAmlo.INIT_SOL_ID Equals tblBranch.SOL_ID _
                     Group Join tblTran In AMLO1059Entity.MTSCTYPE_1_05_9 On tblAmlo.KEY_FLG Equals tblTran.TSCTYPEID
                      Into leftJoinAmlo = Group
                      From tblLeftAmlo In leftJoinAmlo.DefaultIfEmpty
                     Order By tblAmlo.ID Ascending _
                   Select tblAmlo.ID, tblAmlo.KEY_FLG, tblAmlo.P3_TRAN_ID, tblAmlo.P3_TRANS_TYPE, tblAmlo.P3_TRANS_DATE, tblAmlo.P3_TRAN_AMT, tblAmlo.INIT_SOL_ID, tblAmlo.P3_FORACID, tblAmlo.P3_CUST_NAME, tblAmlo.P2_CUST_FNAME, tblAmlo.P2_CUST_LNAME, _
                   tblAmlo.P3_FORACID2, tblAmlo.P3_CUST_NAME2, tblAmlo.P2_CUST_FNAME2, tblAmlo.P2_CUST_LNAME2, TSCTYPEID = If(tblLeftAmlo.TSCTYPEID Is Nothing, "-", tblLeftAmlo.TSCTYPEID), _
                   tblBranch.DEPTCODE, tblBranch.ZONECODE, tblBranch.BRANCHCODE, _
                   tblAmlo.P3_TRAN_CODE, tblLeftAmlo.TSCTYPENAME, tblAmlo.CREATEBY, tblAmlo.CREATEDATE, tblAmlo.UPDATEBY, tblAmlo.UPDATEDATE, _
                   tblAmlo.ISCONFIRM, tblAmlo.ISSEND, tblAmlo.ISDELETE, tblAmlo.DCN_DOCUMENTNO, tblAmlo.RPTREV
            If (UserRight.getRoleGroup > 79) Then 'admin ระบบขึ้นไป

            ElseIf (UserRight.getRoleGroup > 69) Then 'Admin ฝ่าย

            ElseIf (UserRight.getRoleGroup > 59) Then 'Admin เขต 
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup > 49) Then 'Admin สาขา
                'qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup < 50) Then 'user ทั่วไป
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.CREATEBY = UserRight.getUserName)

            End If

            If Not (ddlTransType.SelectedValue = "0") Then
                qryAmlo = qryAmlo.Where(Function(tbl_amlo) tbl_amlo.KEY_FLG = ddlTransType.SelectedValue)
            End If

            If Not (ddlDepartment.SelectedValue = "0") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = ddlDepartment.SelectedValue)
            End If

            If Not (ddlBranch.SelectedValue = "0") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.BRANCHCODE = ddlBranch.SelectedValue)
            End If

            'If Not (txtYear.Text = "") Then
            '    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTYEAR.Value = CInt(txtYear.Text))
            'End If

            If Not (txtNo.Text = "") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DCN_DOCUMENTNO.Contains(txtNo.Text))
            End If

            If rdolTypeDate.SelectedValue = 1 Then
                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text) & " 00:00:00"
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text) & " 23:59:59"
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.P3_TRANS_DATE >= strStartDate And tbl_Amlo.P3_TRANS_DATE <= strEndDate)
                End If
            Else

                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text) & " 00:00:00"
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text) & " 23:59:59"
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.CREATEDATE >= strStartDate And tbl_Amlo.CREATEDATE <= strEndDate)
                End If
            End If


            If Not (rdoUserConfirm.SelectedValue = -1) Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISCONFIRM = rdoUserConfirm.SelectedValue)
            End If

            If Not (rdolIsSend.SelectedValue = 0) Then
                If (rdolIsSend.SelectedValue = 1) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 1)
                ElseIf (rdolIsSend.SelectedValue = 2) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 0)
                End If

            End If

            dsAmlo.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
            If (qryAmlo.Count = 0) Then
                lblMsg.Visible = True
                lblTotal.Text = ""

            Else
                lblMsg.Visible = False
                lblTotal.Text = "จำนวนรายการทั้งหมด " & dsAmlo.Tables("Amlo").Rows.Count & " รายการ "
            End If
        End If

        If (ddlRrptType.SelectedValue = "M57") Then
            grdAmlo1059.Columns(13).Visible = True
            grdAmlo1059.Columns(14).Visible = False
            grdAmlo1059.DataSource = dsAmlo.Tables("Amlo")
            grdAmlo1059.DataBind()
        Else
            grdAmlo.Columns(9).Visible = True
            grdAmlo.Columns(10).Visible = False
            grdAmlo.DataSource = dsAmlo.Tables("Amlo")
            grdAmlo.DataBind()
        End If

        ClearObj()
    End Sub

    Private Sub IsDataBindCompleteExportXML()
        clsDataTable = New clsLINQToDataTable
        clsFormatDate = New clsDateFormat
        Dim UserRight As clsSessionMenuRight
        UserRight = Session("clsMenuRight")

        If dsAmlo.Tables.Contains("Amlo") Then
            dsAmlo.Tables.Clear()
        End If
        NewObjAmlo()

        If (ddlRrptType.SelectedValue = "M51") Then
            Dim qryAmlo = From tblAmlo In AMLO101Entity.AMLO101 Join tblAmloDetail In AMLO101Entity.AMLODETAIL101 On tblAmlo.RPTSEQ Equals tblAmloDetail.RPTSEQ
                         Where tblAmloDetail.RPTIDX = 1 And tblAmlo.ISCOMPLETE = 1 _
                         Order By tblAmlo.RPTSEQ Descending, tblAmlo.DEPTCODE Descending, tblAmlo.BRANCHCODE Descending _
                         Select tblAmlo.RPTYEAR, tblAmlo.RPTREV, tblAmlo.ISSEND, tblAmlo.RPTNO, tblAmloDetail.FIRSTNAME, tblAmloDetail.LASTNAME, tblAmlo.BRANCHCODE, tblAmlo.DEPTCODE, tblAmlo.ZONECODE, tblAmlo.TRANSDATE, tblAmlo.TRANSTYPE, tblAmlo.SIGNTYPE, tblAmlo.UPDATEBY, tblAmlo.SAVEDATE, tblAmlo.RPTSEQ, tblAmlo.ISCOMPLETE, tblAmlo.ISDELETE, tblAmlo.ISCONFIRM, tblAmlo.CREATEBY


            If (UserRight.getRoleGroup > 79) Then 'admin ระบบขึ้นไป
            ElseIf (UserRight.getRoleGroup > 69) Then 'Admin ฝ่าย

                'qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = UserRight.getDeptCode)
            ElseIf (UserRight.getRoleGroup > 59) Then 'Admin เขต 
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup > 49) Then 'Admin สาขา
                'qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup < 50) Then 'user ทั่วไป
                'qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode And tbl_Amlo.CREATEBY = UserRight.getUserName)
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.CREATEBY = UserRight.getUserName)
            End If

            If Not (ddlDepartment.SelectedValue = "0") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = ddlDepartment.SelectedValue)
            End If

            If Not (ddlBranch.SelectedIndex = 0) Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.BRANCHCODE.Contains(ddlBranch.SelectedValue))
            End If

            'If Not (txtYear.Text = "") Then
            '    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTYEAR.Value = CInt(txtYear.Text))
            'End If
            If Not (txtNo.Text = "") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTNO.Contains(txtNo.Text))
            End If

            If rdolTypeDate.SelectedValue = 1 Then
                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.TRANSDATE >= strStartDate And tbl_Amlo.TRANSDATE <= strEndDate)
                End If
            Else

                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SAVEDATE >= strStartDate And tbl_Amlo.SAVEDATE <= strEndDate)
                End If
            End If

            If Not (rdolSignType.SelectedValue = 0) Then
                If (rdolSignType.SelectedValue = 1) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 1)
                ElseIf (rdolSignType.SelectedValue = 2) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 0)
                End If

            End If

            If Not (rdolIsSend.SelectedValue = 0) Then
                If (rdolIsSend.SelectedValue = 1) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 1)
                ElseIf (rdolIsSend.SelectedValue = 2) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 0)
                End If

            End If

            dsAmlo.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
            If (qryAmlo.Count = 0) Then
                lblMsg.Visible = True
                lblTotal.Text = ""

            Else
                lblMsg.Visible = False
                lblTotal.Text = "จำนวนรายการทั้งหมด " & dsAmlo.Tables("Amlo").Rows.Count & " รายการ "
            End If

            'grdAmlo.DataSource = dsAmlo.Tables("Amlo")
            'grdAmlo.DataBind()
            'grdAmlo.Columns(9).Visible = False
            'grdAmlo.Columns(10).Visible = False

        ElseIf (ddlRrptType.SelectedValue = "M52") Then
            Dim qryAmlo = From tblAmlo In AMLO102Entity.AMLO102 Join tblAmloDetail In AMLO102Entity.AMLODETAIL102 On tblAmlo.RPTSEQ Equals tblAmloDetail.RPTSEQ
                         Where tblAmloDetail.RPTIDX = 1 _
                         Order By tblAmlo.RPTSEQ Descending _
                         Select tblAmlo.RPTYEAR, tblAmlo.RPTREV, tblAmlo.ISSEND, tblAmlo.RPTNO, tblAmloDetail.FIRSTNAME, tblAmloDetail.LASTNAME, tblAmlo.BRANCHCODE, tblAmlo.DEPTCODE, tblAmlo.ZONECODE, tblAmlo.TRANSDATE, tblAmlo.TRANSTYPE, tblAmlo.SIGNTYPE, tblAmlo.UPDATEBY, tblAmlo.SAVEDATE, tblAmlo.RPTSEQ, tblAmlo.ISCOMPLETE, tblAmlo.ISDELETE, tblAmlo.ISCONFIRM, tblAmlo.CREATEBY

            If (UserRight.getRoleGroup > 79) Then 'admin ระบบขึ้นไป
            ElseIf (UserRight.getRoleGroup > 69) Then 'Admin ฝ่าย

                '    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = UserRight.getDeptCode)
            ElseIf (UserRight.getRoleGroup > 59) Then 'Admin เขต 
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup > 49) Then 'Admin สาขา
                'qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup < 50) Then 'user ทั่วไป
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.CREATEBY = UserRight.getUserName)

            End If

            If Not (ddlDepartment.SelectedValue = "0") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = ddlDepartment.SelectedValue)
            End If


            If Not (ddlBranch.SelectedIndex = 0) Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.BRANCHCODE.Contains(ddlBranch.SelectedValue))
            End If

            'If Not (txtYear.Text = "") Then
            '    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTYEAR.Value = CInt(txtYear.Text))
            'End If
            If Not (txtNo.Text = "") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTNO.Contains(txtNo.Text))
            End If

            If rdolTypeDate.SelectedValue = 1 Then
                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.TRANSDATE >= strStartDate And tbl_Amlo.TRANSDATE <= strEndDate)
                End If
            Else

                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SAVEDATE >= strStartDate And tbl_Amlo.SAVEDATE <= strEndDate)
                End If
            End If

            If Not (rdolSignType.SelectedValue = 0) Then
                If (rdolSignType.SelectedValue = 1) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 1)
                ElseIf (rdolSignType.SelectedValue = 2) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 0)
                End If

            End If

            If Not (rdolIsSend.SelectedValue = 0) Then
                If (rdolIsSend.SelectedValue = 1) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 1)
                ElseIf (rdolIsSend.SelectedValue = 2) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 0)
                End If

            End If

            dsAmlo.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
            If (qryAmlo.Count = 0) Then
                lblMsg.Visible = True
                lblTotal.Text = ""

            Else
                lblMsg.Visible = False
                lblTotal.Text = "จำนวนรายการทั้งหมด " & dsAmlo.Tables("Amlo").Rows.Count & " รายการ "
            End If


        ElseIf (ddlRrptType.SelectedValue = "M53") Then
            Dim qryAmlo = From tblAmlo In AMLO103Entity.AMLO103 Join tblAmloDetail In AMLO103Entity.AMLODETAIL103 On tblAmlo.RPTSEQ Equals tblAmloDetail.RPTSEQ
                       Where tblAmloDetail.RPTIDX = 1 And tblAmlo.RPTTYPE = 3 And tblAmlo.ISCOMPLETE = 1 _
                       Order By tblAmlo.RPTSEQ Descending _
                       Select tblAmlo.RPTYEAR, tblAmlo.RPTREV, tblAmlo.ISSEND, tblAmlo.RPTNO, tblAmloDetail.FIRSTNAME, tblAmloDetail.LASTNAME, tblAmlo.BRANCHCODE, tblAmlo.DEPTCODE, tblAmlo.ZONECODE, tblAmlo.TRANSDATE, tblAmlo.TRANSTYPE, signtype = If(tblAmlo.SIGNTYPE Is Nothing, 0, tblAmlo.SIGNTYPE), tblAmlo.UPDATEBY, tblAmlo.SAVEDATE, tblAmlo.RPTSEQ, tblAmlo.ISCOMPLETE, tblAmlo.ISDELETE, tblAmlo.ISCONFIRM, tblAmlo.CREATEBY

            If (UserRight.getRoleGroup > 79) Then 'admin ระบบขึ้นไป

            ElseIf (UserRight.getRoleGroup > 69) Then 'Admin ฝ่าย

            ElseIf (UserRight.getRoleGroup > 59) Then 'Admin เขต 
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup > 49) Then 'Admin สาขา
                'qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup < 50) Then 'user ทั่วไป
                'qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode And tbl_Amlo.CREATEBY = UserRight.getUserName)
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.CREATEBY = UserRight.getUserName)
            End If

            If Not (ddlDepartment.SelectedValue = "0") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = ddlDepartment.SelectedValue)
            End If

            If Not (ddlBranch.SelectedValue = "0") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.BRANCHCODE = ddlBranch.SelectedValue)
            End If

            'If Not (txtYear.Text = "") Then
            '    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTYEAR.Value = CInt(txtYear.Text))
            'End If

            If Not (txtNo.Text = "") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTNO.Contains(txtNo.Text))
            End If

            If rdolTypeDate.SelectedValue = 1 Then
                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.TRANSDATE >= strStartDate And tbl_Amlo.TRANSDATE <= strEndDate)
                End If
            Else

                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SAVEDATE >= strStartDate And tbl_Amlo.SAVEDATE <= strEndDate)
                End If
            End If



            If Not (rdolIsSend.SelectedValue = 0) Then
                If (rdolIsSend.SelectedValue = 1) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 1)
                ElseIf (rdolIsSend.SelectedValue = 2) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 0)
                End If

            End If

            dsAmlo.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
            If (qryAmlo.Count = 0) Then
                lblMsg.Visible = True
                lblTotal.Text = ""

            Else
                lblMsg.Visible = False
                lblTotal.Text = "จำนวนรายการทั้งหมด " & dsAmlo.Tables("Amlo").Rows.Count & " รายการ "
            End If
        ElseIf (ddlRrptType.SelectedValue = "M57") Then
            Dim qryAmlo = From tblAmlo In AMLO1059Entity.AMLO1059 Join tblBranch In AMLO1059Entity.SBRANCHes _
                      On tblAmlo.INIT_SOL_ID Equals tblBranch.SOL_ID _
                     Group Join tblTran In AMLO1059Entity.MTSCTYPE_1_05_9 On tblAmlo.KEY_FLG Equals tblTran.TSCTYPEID
                      Into leftJoinAmlo = Group
                      From tblLeftAmlo In leftJoinAmlo.DefaultIfEmpty
                     Order By tblAmlo.ID Ascending _
                   Select tblAmlo.ID, tblAmlo.KEY_FLG, tblAmlo.P3_TRAN_ID, tblAmlo.P3_TRANS_TYPE, tblAmlo.P3_TRANS_DATE, tblAmlo.P3_TRAN_AMT, tblAmlo.INIT_SOL_ID, tblAmlo.P3_FORACID, tblAmlo.P3_CUST_NAME, tblAmlo.P2_CUST_FNAME, tblAmlo.P2_CUST_LNAME, _
                   tblAmlo.P3_FORACID2, tblAmlo.P3_CUST_NAME2, tblAmlo.P2_CUST_FNAME2, tblAmlo.P2_CUST_LNAME2, TSCTYPEID = If(tblLeftAmlo.TSCTYPEID Is Nothing, "-", tblLeftAmlo.TSCTYPEID), _
                   tblBranch.DEPTCODE, tblBranch.ZONECODE, tblBranch.BRANCHCODE, _
                   tblAmlo.P3_TRAN_CODE, tblLeftAmlo.TSCTYPENAME, tblAmlo.CREATEBY, tblAmlo.CREATEDATE, tblAmlo.UPDATEBY, tblAmlo.UPDATEDATE, _
                   tblAmlo.ISCONFIRM, tblAmlo.ISSEND, tblAmlo.ISDELETE, tblAmlo.DCN_DOCUMENTNO, tblAmlo.RPTREV

            If (UserRight.getRoleGroup > 79) Then 'admin ระบบขึ้นไป

            ElseIf (UserRight.getRoleGroup > 69) Then 'Admin ฝ่าย

            ElseIf (UserRight.getRoleGroup > 59) Then 'Admin เขต 
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup > 49) Then 'Admin สาขา
                'qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode)

            ElseIf (UserRight.getRoleGroup < 50) Then 'user ทั่วไป
                'qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ZONECODE = UserRight.getZoneCode And tbl_Amlo.CREATEBY = UserRight.getUserName)
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.CREATEBY = UserRight.getUserName)
            End If

            If Not (ddlDepartment.SelectedValue = "0") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = ddlDepartment.SelectedValue)
            End If

            If Not (ddlBranch.SelectedValue = "0") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.BRANCHCODE = ddlBranch.SelectedValue)
            End If

            'If Not (txtYear.Text = "") Then
            '    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTYEAR.Value = CInt(txtYear.Text))
            'End If

            If Not (txtNo.Text = "") Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DCN_DOCUMENTNO.Contains(txtNo.Text))
            End If

            If rdolTypeDate.SelectedValue = 1 Then
                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text) & " 00:00:00"
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text) & " 23:59:59"
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.P3_TRANS_DATE >= strStartDate And tbl_Amlo.P3_TRANS_DATE <= strEndDate)
                End If
            Else

                If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                    Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text) & " 00:00:00"
                    Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text) & " 23:59:59"
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.CREATEDATE >= strStartDate And tbl_Amlo.CREATEDATE <= strEndDate)
                End If
            End If


            If Not (rdoUserConfirm.SelectedValue = -1) Then
                qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISCONFIRM = rdoUserConfirm.SelectedValue)
            End If

            If Not (rdolIsSend.SelectedValue = 0) Then
                If (rdolIsSend.SelectedValue = 1) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 1)
                ElseIf (rdolIsSend.SelectedValue = 2) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 0)
                End If

            End If

            dsAmlo.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
            If (qryAmlo.Count = 0) Then
                lblMsg.Visible = True
                lblTotal.Text = ""

            Else
                lblMsg.Visible = False
                lblTotal.Text = "จำนวนรายการทั้งหมด " & dsAmlo.Tables("Amlo").Rows.Count & " รายการ "
            End If
        End If

        If (ddlRrptType.SelectedValue = "M57") Then
            grdAmlo1059.Columns(13).Visible = False
            grdAmlo1059.Columns(14).Visible = True
            grdAmlo1059.DataSource = dsAmlo.Tables("Amlo")
            grdAmlo1059.DataBind()
        Else
            grdAmlo.Columns(9).Visible = False
            grdAmlo.Columns(10).Visible = True
            grdAmlo.DataSource = dsAmlo.Tables("Amlo")
            grdAmlo.DataBind()
        End If

        ClearObj()
    End Sub

    Private Sub bindToGrid()

    End Sub


    Private Sub clearGvIndex()
        grdAmlo.SelectedIndex = -1
    End Sub

    Private Function GetSortDirection(ByVal column As String) As String

        ' By default, set the sort direction to ascending.
        Dim sortDirection = "DESC"

        ' Retrieve the last column that was sorted.
        Dim sortExpression = TryCast(ViewState("SortExpression"), String)

        If sortExpression IsNot Nothing Then
            ' Check if the same column is being sorted.
            ' Otherwise, the default value can be returned.
            If sortExpression = column Then
                Dim lastDirection = TryCast(ViewState("SortDirection"), String)
                If lastDirection IsNot Nothing _
                  AndAlso lastDirection = "DESC" Then

                    sortDirection = "ASC"

                End If
            End If
        End If

        ' Save new values in ViewState.
        ViewState("SortDirection") = sortDirection
        ViewState("SortExpression") = column
        Return sortDirection
    End Function

    Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click


        Dim strConvertDateStart As String
        Dim strConvertDateEnd As String

        'If Not (txtDateStart.Text = "" Or txtDateEnd.Text = "") Then
        '    Dim tempStart() As String = txtDateStart.Text.Split("/")
        '    Dim tempEnd() As String = txtDateEnd.Text.Split("/")

        '    If (tempStart(2) > 2500) Then
        '        strConvertDateStart = tempStart(0) & "/" & tempStart(1) & "/" & tempStart(2) - 543

        '    Else
        '        strConvertDateStart = txtDateStart.Text
        '    End If

        '    If (tempEnd(2) > 2500) Then
        '        strConvertDateEnd = tempEnd(0) & "/" & tempEnd(1) & "/" & tempEnd(2) - 543
        '    Else
        '        strConvertDateEnd = txtDateEnd.Text
        '    End If
        'Else
        '    strConvertDateStart = ""
        '    strConvertDateEnd = ""

        'End If





        'Dim param As String = "{ 'RPTTYPE' : '" & ddlRrptType.SelectedValue & "'," _
        '                              & "'RPTYEAR' : '" & txtYear.Text.Trim & "'," _
        '                              & "'DEPTCODE' : '" & ddlDepartment.SelectedValue & "'," _
        '                              & "'BRANCHCODE' : '" & ddlBranch.SelectedValue & "'," _
        '                              & "'RPTNO' : '" & txtNo.Text.Trim & "'," _
        '                              & "'TYPEDATE' : '" & rdolTypeDate.SelectedValue & "'," _
        '                              & "'STARTDATE' : '" & strConvertDateStart & "'," _
        '                              & "'ENDDATE' : '" & strConvertDateEnd & "'," _
        '                              & "'SIGNTYPE' : '" & If(rdolSignType.SelectedValue = 0, "", rdolSignType.SelectedValue) & "'," _
        '                              & "'ISSEND' : '" & If(rdolIsSend.SelectedValue = 0, "", rdolIsSend.SelectedValue) & "'}"
        Dim param As String = "{ 'RPTTYPE' : '" & ddlRrptType.SelectedValue & "'," _
                                     & "'TRANSTYPE' : '" & ddlTransType.SelectedValue & "'," _
                                     & "'DEPTCODE' : '" & ddlDepartment.SelectedValue & "'," _
                                     & "'BRANCHCODE' : '" & ddlBranch.SelectedValue & "'," _
                                     & "'RPTNO' : '" & txtNo.Text.Trim & "'," _
                                     & "'TYPEDATE' : '" & rdolTypeDate.SelectedValue & "'," _
                                     & "'STARTDATE' : '" & txtDateStart.Text.Trim & "'," _
                                     & "'ENDDATE' : '" & txtDateEnd.Text.Trim & "'," _
                                     & "'SIGNTYPE' : '" & If(rdolSignType.SelectedValue = 0, "", rdolSignType.SelectedValue) & "'," _
                                     & "'ISSEND' : '" & If(rdolIsSend.SelectedValue = 0, "", rdolIsSend.SelectedValue) & "'}"
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType, "key", _
               "OpenWindowWithPost('../Form/frmReportAmlo.aspx','width=730,height=345,left=100,top=100,resizable=yes,scrollbars=yes','Report', " + param + ");", True)


        'ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType, "key", _
        '        "OpenWindowWithPost('../Form/frmRptAmlo.aspx','width=730,height=345,left=100,top=100,resizable=yes,scrollbars=yes','Report', " + param + ");", True)

        'Else
        'Dim param As String = "{ 'RPTSEQ' : '" & "" & "','RPTTYPE' : '" & "2" & "'}"

        'ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType, "key", "OpenWindowWithPost('frmPopupSelectPrint.aspx','width=730,height=345,left=100,top=100,resizable=yes,scrollbars=yes','Report', " + param + ");", True)

        'End If


    End Sub
#End Region



    Public Sub enableBtn(ByVal usebtn As enmUseWork)
        If (usebtn = enmUseWork.report) Then
            'btnPrint.Visible = True
            If (ddlRrptType.SelectedValue = "M57") Then
                btnPrintFull.Visible = True
                btnPrint1.Visible = True
                btnPrint2.Visible = True
            Else
                btnPrint.Visible = True
            End If

            btnGenXML.Visible = False
            btnGenXMLShort.Visible = False
            btnGenXMLACID.Visible = False
            btnGenXMLNoACID.Visible = False
            btnGenCtlFile.Visible = False
            btnComplete.Visible = False
            If Not AMLO101Entity Is Nothing Then
                IsDataBindReport()
            End If
            ' grdAmlo.AllowPaging = True
        ElseIf (usebtn = enmUseWork.ExportXML) Then
            btnPrint.Visible = False
            btnPrintFull.Visible = False
            btnPrint1.Visible = False
            btnPrint2.Visible = False

            If (ddlRrptType.SelectedValue = "M57") Then
                btnGenXML.Visible = False
                btnGenXMLACID.Visible = True
                btnGenXMLNoACID.Visible = True
                btnGenXMLShort.Visible = False
                btnGenCtlFile.Visible = True
            Else
                btnGenXML.Visible = True
                btnGenXMLShort.Visible = True
                btnGenXMLACID.Visible = False
                btnGenXMLNoACID.Visible = False
                btnGenCtlFile.Visible = False
            End If
            btnComplete.Visible = False
            grdAmlo.Columns(9).Visible = True
            grdAmlo.Columns(10).Visible = False
            'If Not AMLO101Entity Is Nothing Then
            '    IsDataBindExportXML()
            'End If
            ' grdAmlo.AllowPaging = False
        ElseIf (usebtn = enmUseWork.CompleteExportXML) Then
            btnPrint.Visible = False
            btnPrintFull.Visible = False
            btnPrint1.Visible = False
            btnPrint2.Visible = False
            btnGenXML.Visible = False
            btnGenXMLShort.Visible = False
            btnGenXMLACID.Visible = False
            btnGenXMLNoACID.Visible = False
            btnGenCtlFile.Visible = False
            btnComplete.Visible = True
            'If Not AMLO101Entity Is Nothing Then
            '    IsDataBindCompleteExportXML()
            'End If
            grdAmlo.Columns(9).Visible = False
            grdAmlo.Columns(10).Visible = True
            ' grdAmlo.AllowPaging = False
        End If
    End Sub
    Sub DisplayDownloadDialog(ByVal PathVirtual As String)

        Dim strPhysicalPath As String
        Dim objFileInfo As System.IO.FileInfo
        Try
            strPhysicalPath = Server.MapPath(PathVirtual)
            'exit if file does not exist
            If Not System.IO.File.Exists(strPhysicalPath) _
                 Then Exit Sub
            objFileInfo = New System.IO.FileInfo(strPhysicalPath)
            Response.Clear()
            'Add Headers to enable dialog display
            Response.AddHeader("Content-Disposition", "attachment; filename=" & _
                objFileInfo.Name)
            Response.AddHeader("Content-Length", objFileInfo.Length.ToString())

            Response.ContentType = "application/octet-stream"
            Response.WriteFile(objFileInfo.FullName)


        Catch
            'on exception take no action
            'you can implement differently
        Finally

            Response.End()

        End Try
    End Sub
    Protected Sub btnGenXML_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGenXML.Click

        Dim dsAmloXML As DataSet
        Dim dsAmloXMLDetail As DataSet
        Dim dsAmloXMLCount As DataSet
        clsFormatDate = New clsDateFormat

        Try
            ' ========================================
            If (ddlRrptType.SelectedValue = "M57") Then
                If (grdAmlo1059.Rows.Count = 0) Then
                    ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                             UpdatePanel1.GetType(), _
                                                             "clientScript", _
                                                             "alert('กรุณาเลือกข้อมูลที่ต้องการออกไฟล์ Xml ')", _
                                                             True)
                    Exit Sub
                End If
            Else
                If (grdAmlo.Rows.Count = 0) Then
                    ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                             UpdatePanel1.GetType(), _
                                                             "clientScript", _
                                                             "alert('กรุณาเลือกข้อมูลที่ต้องการออกไฟล์ Xml ')", _
                                                             True)
                    Exit Sub
                End If
            End If

            clsDataTable = New clsLINQToDataTable
            clsFormatDate = New clsDateFormat
            Dim UserRight As clsSessionMenuRight
            UserRight = Session("clsMenuRight")

            dsAmloXML = New DataSet()

            NewObjAmlo()

            If (ddlRrptType.SelectedValue = "M51") Then
                Dim qryAmlo = From tblAmlo In AMLO101Entity.AMLO101 Join tblAmloDetail In AMLO101Entity.AMLODETAIL101 On tblAmlo.RPTSEQ Equals tblAmloDetail.RPTSEQ
                             Where tblAmloDetail.RPTIDX = 1 And tblAmlo.ISCOMPLETE = 1 _
                             Order By tblAmlo.RPTSEQ Descending, tblAmlo.DEPTCODE Descending, tblAmlo.BRANCHCODE Descending _
                             Select tblAmlo.RPTSEQ, tblAmlo.GHBCODE, tblAmlo.RPTYEAR, tblAmlo.RPTNO, tblAmlo.RPTREV, tblAmlo.BRANCHCODE, tblAmlo.SOL_ID, _
tblAmlo.DEPTCODE, tblAmlo.RPTDATE, tblAmlo.TRANSDATE, tblAmlo.TRANSTYPE, tblAmlo.TRANSTYPEOTHER, _
tblAmlo.ACCOUNTNO, tblAmlo.TRANSAMT, tblAmlo.CURRENCY, tblAmlo.GAINPREFIX, tblAmlo.GAINFIRSTNAME, tblAmlo.GAINMIDNAME, _
tblAmlo.GAINLASTNAME, tblAmlo.OBJBIZ, tblAmlo.OBJBIZOTHER, tblAmlo.SAVETYPE, tblAmlo.SIGNTYPE, tblAmlo.SAVEDATE, _
tblAmlo.UPDATEBY, tblAmlo.ISCOMPLETE, tblAmlo.ISDELETE, tblAmlo.ISCONFIRM, tblAmlo.ISSEND, _
tblAmloDetail.RPTIDX, tblAmloDetail.RPTTYPE, tblAmloDetail.RPTIDXTYPE, _
tblAmloDetail.UNIQUETYPE, tblAmloDetail.UNIQUEID, tblAmloDetail.PREFIX, tblAmloDetail.FIRSTNAME, _
                tblAmloDetail.MIDNAME, tblAmloDetail.LASTNAME, tblAmloDetail.FORMALADDR, tblAmloDetail.FORMALPHONE, _
                tblAmloDetail.FORMALFAX, tblAmloDetail.OCCUPATIONTYPE, tblAmloDetail.OCCUPATIONTYPEOTHER, _
                tblAmloDetail.WORKADDR, tblAmloDetail.WORKPHONE, tblAmloDetail.WORKFAX, tblAmloDetail.BIZTYPE, _
                tblAmloDetail.BIZTYPEOTHER, tblAmloDetail.CONTACTADDR, tblAmloDetail.CONTACTPHONE, _
                tblAmloDetail.CONTACTFAX, tblAmloDetail.CUSTIDTYPE, tblAmloDetail.CUSTIDTYPEOTHER, _
                tblAmloDetail.CUSTID, tblAmloDetail.EVBY, tblAmloDetail.EVGEN, tblAmloDetail.EVGENTEXT, _
                tblAmloDetail.EVEXPIRE, tblAmloDetail.EVEXPIRETEXT

                'Select tblAmlo.CURRENCY, tblAmlo.TRANSAMT, tblAmlo.ACCOUNTNO, tblAmlo.TRANSTYPEOTHER, tblAmloDetail.EVBY, _
                'tblAmloDetail.EVGEN, tblAmloDetail.EVEXPIRE, tblAmloDetail.CUSTIDTYPE, tblAmloDetail.CUSTIDTYPEOTHER, _
                'tblAmloDetail.CUSTID, tblAmloDetail.OCCUPATIONTYPE, tblAmloDetail.OCCUPATIONTYPEOTHER, tblAmloDetail.CONTACTADDR, _
                'tblAmloDetail.CONTACTPHONE, tblAmloDetail.CONTACTFAX, tblAmloDetail.FORMALADDR, tblAmloDetail.FORMALPHONE, _
                'tblAmloDetail.FORMALFAX, tblAmloDetail.WORKADDR, tblAmloDetail.WORKPHONE, tblAmloDetail.WORKFAX, tblAmlo.RPTNO, _
                'tblAmlo.RPTREV, tblAmlo.RPTDATE, tblAmlo.RPTYEAR, tblAmlo.ISSEND, tblAmloDetail.PREFIX, tblAmloDetail.FIRSTNAME, _
                'tblAmloDetail.MIDNAME, tblAmloDetail.LASTNAME, tblAmloDetail.UNIQUETYPE, tblAmloDetail.UNIQUEID, tblAmlo.BRANCHCODE, _
                'tblAmlo.TRANSDATE, tblAmlo.TRANSTYPE, tblAmlo.SIGNTYPE, tblAmlo.UPDATEBY, tblAmlo.SAVEDATE, tblAmlo.RPTSEQ, _
                'tblAmlo.ISCOMPLETE, tblAmlo.ISDELETE, tblAmlo.ISCONFIRM, tblAmlo.GHBCODE

                If Not (ddlDepartment.SelectedValue = "0") Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = ddlDepartment.SelectedValue)
                End If

                If Not (ddlBranch.SelectedIndex = 0) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.BRANCHCODE.Contains(ddlBranch.SelectedValue))
                End If

                'If Not (txtYear.Text = "") Then
                '    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTYEAR.Value = CInt(txtYear.Text))
                'End If

                If Not (txtNo.Text = "") Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTNO.Contains(txtNo.Text))
                End If

                If rdolTypeDate.SelectedValue = 1 Then
                    If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                        Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                        Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.TRANSDATE >= strStartDate And tbl_Amlo.TRANSDATE <= strEndDate)
                    End If
                Else
                    If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                        Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                        Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SAVEDATE >= strStartDate And tbl_Amlo.SAVEDATE <= strEndDate)
                    End If
                End If

                If Not (rdolSignType.SelectedValue = 0) Then
                    If (rdolSignType.SelectedValue = 1) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 1)
                    ElseIf (rdolSignType.SelectedValue = 2) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 0)
                    End If

                End If

                If Not (rdolIsSend.SelectedValue = 0) Then
                    If (rdolIsSend.SelectedValue = 1) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 1)
                    ElseIf (rdolIsSend.SelectedValue = 2) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 0)
                    End If

                End If

                dsAmloXML.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
                If (qryAmlo.Count = 0) Then
                    lblMsg.Visible = True
                Else
                    lblMsg.Visible = False
                End If

                Dim AmloXML As New clsAmloXML
                Dim path As String
                Dim iCount, iAllRec As Integer
                Dim CountChk As Integer = 0
                Dim CountChkToGen As Integer = 0
                Dim nAmlo1Data As clsAmlo1Data = New clsAmlo1Data
                Dim nAmlo1Psn(9) As clsAmlo1Psn
                iAllRec = dsAmloXML.Tables("Amlo").Rows.Count

                For iCount = 0 To iAllRec - 1
                    Dim chkConfirmGen As CheckBox = CType(grdAmlo.Rows(iCount).FindControl("CheckBox1"), CheckBox)
                    If (chkConfirmGen.Checked) Then
                        CountChk += 1
                    End If
                Next
                path = "C:\EXPORTXML\AMLO101.xml"
                FileCopy("C:\EXPORTXML\Template\AMLO101.xml", "C:\EXPORTXML\AMLO101.xml")
                For iCount = 0 To iAllRec - 1

                    Dim chkConfirmGen As CheckBox = CType(grdAmlo.Rows(iCount).FindControl("CheckBox1"), CheckBox)
                    If (chkConfirmGen.Checked) Then
                        CountChkToGen += 1
                        'ชนิดของรายงาน 1-01
                        nAmlo1Data.p01_type = "1-01"
                        'เลขที่รายงาน 
                        Dim strRptNo As String = ""

                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("GHBCODE").ToString
                        'strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("DEPTCODE").ToString
                        'strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("BRANCHCODE").ToString.Substring(0, 3)
                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("SOL_ID").ToString
                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTYEAR").ToString
                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTNO").ToString


                        nAmlo1Data.p02_reportid = strRptNo
                        'ครั้งที่แก้ไข/เพิ่มเติม  -ถ้าไม่ระบุจะถือเป็นฉบับแรก
                        nAmlo1Data.p03_revision = dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTREV").ToString
                        'วันที่รายงาน -วันที่/เดือน/ปี พ.ศ.(4หลัก)
                        nAmlo1Data.p04_date = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTDATE").ToString, enmTypedata.typeDate)
                        '=================================
                        'ส่วนที่ 1. ผู้ทำธุรกรรม 
                        nAmlo1Data.p101_cnd_name_prefix = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("PREFIX").ToString, enmTypedata.typeString)
                        nAmlo1Data.p102_cnd_name_first = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FIRSTNAME").ToString, enmTypedata.typeString)
                        nAmlo1Data.p103_cnd_name_middle = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("MIDNAME").ToString, enmTypedata.typeString)
                        nAmlo1Data.p104_cnd_name_last = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("LASTNAME").ToString, enmTypedata.typeString)

                        nAmlo1Data.p105_cnd_id_no = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("UNIQUEID").ToString, enmTypedata.typeString)
                        nAmlo1Data.p106_cnd_id_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("UNIQUETYPE").ToString, enmTypedata.typeString)

                        nAmlo1Data.p107_cnd_location_addr = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FORMALADDR").ToString, enmTypedata.typeString)
                        nAmlo1Data.p108_cnd_location_phone = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FORMALPHONE").ToString, enmTypedata.typeString)
                        nAmlo1Data.p109_cnd_location_fax = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FORMALFAX").ToString, enmTypedata.typeString)


                        'ระบุอาชีพ 1-เจ้าของกิจการ, 2-ข้าราชการ, 3-รับจ้าง, 4-พนักงานรัฐวิสาหกิจ, 5-อาชีพอิสระ, 6-นิสิต นักศึกษา, 99-อื่นๆ
                        nAmlo1Data.p110_cnd_office_occ = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OCCUPATIONTYPE").ToString, enmTypedata.typeString)
                        nAmlo1Data.p111_cnd_office_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OCCUPATIONTYPEOTHER").ToString, enmTypedata.typeString)
                        nAmlo1Data.p112_cnd_office_org = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("WORKADDR").ToString, enmTypedata.typeString)
                        nAmlo1Data.p113_cnd_office_phone = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("WORKPHONE").ToString, enmTypedata.typeString)
                        nAmlo1Data.p114_cnd_office_fax = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("WORKFAX").ToString, enmTypedata.typeString)


                        nAmlo1Data.p115_cnd_contact_addr = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CONTACTADDR").ToString, enmTypedata.typeString)
                        nAmlo1Data.p116_cnd_contact_phone = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CONTACTPHONE").ToString, enmTypedata.typeString)
                        nAmlo1Data.p117_cnd_contact_fax = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CONTACTFAX").ToString, enmTypedata.typeString)


                        'ชนิดหลักฐาน 1-บัตรประจำตัวประชาชน (ต้องเป็นตัวเลขและมีขนาด 13 หลักเท่านั้น), 2-บัตรประจำตัวผู้เสียภาษีอากร (ต้องเป็นตัวเลขและมีขนาด 10 หลักเท่านั้น), 3-หนังสือเดินทาง, 4-ใบสำคัญคนต่างด้าว,
                        ' 5- หนังสือรับรองข้อความในทะเบียน, 6 - บัตรที่ออกโดยองค์กรรัฐ, 7 - หนังสือสุทธิสำหรับพระภิกษุ, 8 -บัตรประจำตัวชนกลุ่มน้อย, 99 - อื่นๆ 
                        nAmlo1Data.p118_cnd_iddoc_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CUSTIDTYPE").ToString, enmTypedata.typeString)
                        nAmlo1Data.p119_cnd_iddoc_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CUSTIDTYPEOTHER").ToString, enmTypedata.typeString)
                        nAmlo1Data.p120_cnd_iddoc_no = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CUSTID").ToString, enmTypedata.typeString)

                        nAmlo1Data.p121_cnd_iddoc_by = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVBY").ToString, enmTypedata.typeString)

                        Try

                            ' nAmlo1Data.p122_cnd_iddoc_date = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGEN").ToString, enmTypedata.typeDate)
                            nAmlo1Data.p122_cnd_iddoc_date = setValueDefault(If(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGENTEXT").ToString = "-", dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGENTEXT").ToString, clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGENTEXT").ToString)), enmTypedata.typeother)
                        Catch ex As Exception

                        End Try

                        Try
                            'nAmlo1Data.p123_cnd_iddoc_expire = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRE").ToString, enmTypedata.typeDate)
                            nAmlo1Data.p123_cnd_iddoc_expire = setValueDefault(If(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRETEXT").ToString = "-", dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRETEXT").ToString, clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRETEXT").ToString)), enmTypedata.typeother)
                        Catch ex As Exception

                        End Try


                        'ส่วนที่ 3. ข้อเท็จจริงเกี่ยวกับธุรกรรม

                        nAmlo1Data.p301_tsc_date = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSDATE").ToString, enmTypedata.typeDate)
                        nAmlo1Data.p302_tsc_item_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSTYPE").ToString, enmTypedata.typeString)
                        nAmlo1Data.p303_tsc_item_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSTYPEOTHER").ToString, enmTypedata.typeString)
                        nAmlo1Data.p304_tsc_item_acccno = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCOUNTNO").ToString, enmTypedata.typeString)
                        nAmlo1Data.p305_tsc_item_cur = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CURRENCY").ToString, enmTypedata.typeString)
                        nAmlo1Data.p306_tsc_item_amt = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSAMT").ToString, enmTypedata.typeString)
                        nAmlo1Data.p307_tsc_name_prefix = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINPREFIX").ToString, enmTypedata.typeString)
                        nAmlo1Data.p308_tsc_name_first = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINFIRSTNAME").ToString, enmTypedata.typeString)
                        nAmlo1Data.p309_tsc_name_middle = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINMIDNAME").ToString, enmTypedata.typeString)
                        nAmlo1Data.p310_tsc_name_last = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINLASTNAME").ToString, enmTypedata.typeString)
                        nAmlo1Data.p311_tsc_obj_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OBJBIZ").ToString, enmTypedata.typeString)
                        nAmlo1Data.p312_tsc_obj_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OBJBIZOTHER").ToString, enmTypedata.typeString)
                        nAmlo1Data.p401_record_writer = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("SAVETYPE").ToString, enmTypedata.typeString)
                        nAmlo1Data.p402_record_sign = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("SIGNTYPE").ToString, enmTypedata.typeString)
                        nAmlo1Data.p403_record_date = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("SAVEDATE").ToString, enmTypedata.typeDate)


                        Dim mRptSeq As Integer = Convert.ToInt32(dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTSEQ").ToString)
                        Dim qryAmloDetailCount = From tblAmloDetail In AMLO101Entity.AMLODETAIL101
                         Where tblAmloDetail.RPTSEQ = mRptSeq And tblAmloDetail.RPTIDX > 1
                         Group By tblAmloDetail.RPTSEQ Into MaxDetail = Max(tblAmloDetail.RPTIDX)

                        dsAmloXMLCount = New DataSet()
                        '    NewObjAmloDetail()
                        dsAmloXMLCount.Tables.Add(clsDataTable.LINQToDataTable(qryAmloDetailCount, "AmloCount").Copy)


                        Dim iPsnCount As Integer = 0
                        If dsAmloXMLCount.Tables("AmloCount").Rows.Count > 0 Then
                            Dim mMaxRptDetail As Integer = Convert.ToInt32(dsAmloXMLCount.Tables("AmloCount").Rows(0).Item("MaxDetail").ToString)
                            Dim qryAmloDetail = From tblAmloDetail In AMLO101Entity.AMLODETAIL101
                            Where tblAmloDetail.RPTSEQ = mRptSeq And tblAmloDetail.RPTIDX > 1
                            Order By tblAmloDetail.RPTIDX
                             Select tblAmloDetail.EVBY, tblAmloDetail.EVGEN, tblAmloDetail.EVEXPIRE, tblAmloDetail.CUSTIDTYPE, tblAmloDetail.CUSTIDTYPEOTHER, _
                                tblAmloDetail.CUSTID, tblAmloDetail.OCCUPATIONTYPE, tblAmloDetail.OCCUPATIONTYPEOTHER, tblAmloDetail.CONTACTADDR, _
                                 tblAmloDetail.CONTACTPHONE, tblAmloDetail.CONTACTFAX, tblAmloDetail.FORMALADDR, tblAmloDetail.FORMALPHONE, tblAmloDetail.BIZTYPE, _
                            tblAmloDetail.FORMALFAX, tblAmloDetail.WORKADDR, tblAmloDetail.WORKPHONE, tblAmloDetail.WORKFAX, tblAmloDetail.RPTIDXTYPE, _
                            tblAmloDetail.PREFIX, tblAmloDetail.FIRSTNAME, tblAmloDetail.MIDNAME, tblAmloDetail.LASTNAME, tblAmloDetail.UNIQUETYPE, tblAmloDetail.UNIQUEID

                            dsAmloXMLDetail = New DataSet()
                            dsAmloXMLDetail.Tables.Add(clsDataTable.LINQToDataTable(qryAmloDetail, "AmloDetail").Copy)

                            For Each dr As DataRow In dsAmloXMLDetail.Tables("AmloDetail").Rows
                                iPsnCount = iPsnCount + 1
                                nAmlo1Psn(iPsnCount) = New clsAmlo1Psn(iPsnCount)

                                nAmlo1Psn(iPsnCount).p201_psn_relation = setValueDefault(dr("RPTIDXTYPE").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p202_psn_name_prefix = setValueDefault(dr("PREFIX").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p203_psn_name_first = setValueDefault(dr("FIRSTNAME").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p204_psn_name_middle = setValueDefault(dr("MIDNAME").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p205_psn_name_last = setValueDefault(dr("LASTNAME").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p206_psn_id_no = setValueDefault(dr("UNIQUEID").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p207_psn_id_type = setValueDefault(dr("UNIQUETYPE").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p208_psn_location_addr = setValueDefault(dr("FORMALADDR").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p209_psn_location_phone = setValueDefault(dr("FORMALPHONE").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p210_psn_location_fax = setValueDefault(dr("FORMALFAX").ToString, enmTypedata.typeString)


                                'ระบุอาชีพ 1-เจ้าของกิจการ, 2-ข้าราชการ, 3-รับจ้าง, 4-พนักงานรัฐวิสาหกิจ, 5-อาชีพอิสระ, 6-นิสิต นักศึกษา, 99-อื่นๆ
                                nAmlo1Psn(iPsnCount).p211_psn_office_occ = setValueDefault(dr("OCCUPATIONTYPE").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p212_psn_office_org = setValueDefault(dr("WORKADDR").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p213_psn_office_phone = setValueDefault(dr("WORKPHONE").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p214_psn_office_fax = setValueDefault(dr("WORKFAX").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p215_psn_office_type = setValueDefault(dr("BIZTYPE").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p216_psn_office_desc = setValueDefault(dr("OCCUPATIONTYPEOTHER").ToString, enmTypedata.typeString)

                                nAmlo1Psn(iPsnCount).p217_psn_contact_addr = setValueDefault(dr("CONTACTADDR").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p218_psn_contact_phone = setValueDefault(dr("CONTACTPHONE").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p219_psn_contact_fax = setValueDefault(dr("CONTACTFAX").ToString, enmTypedata.typeString)

                                'ชนิดหลักฐาน 1-บัตรประจำตัวประชาชน (ต้องเป็นตัวเลขและมีขนาด 13 หลักเท่านั้น), 2-บัตรประจำตัวผู้เสียภาษีอากร (ต้องเป็นตัวเลขและมีขนาด 10 หลักเท่านั้น), 3-หนังสือเดินทาง, 4-ใบสำคัญคนต่างด้าว,
                                ' 5- หนังสือรับรองข้อความในทะเบียน, 6 - บัตรที่ออกโดยองค์กรรัฐ, 7 - หนังสือสุทธิสำหรับพระภิกษุ, 8 -บัตรประจำตัวชนกลุ่มน้อย, 99 - อื่นๆ 

                                nAmlo1Psn(iPsnCount).p220_psn_iddoc_type = setValueDefault(dr("CUSTIDTYPE").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p221_psn_iddoc_desc = setValueDefault(dr("CUSTIDTYPEOTHER").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p222_psn_iddoc_no = setValueDefault(dr("CUSTID").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p223_psn_iddoc_by = setValueDefault(dr("EVBY").ToString, enmTypedata.typeString)
                                Try
                                    nAmlo1Psn(iPsnCount).p224_psn_iddoc_date = setValueDefault(If(dr("EVGENTEXT").ToString = "-", dr("EVGENTEXT").ToString, clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dr("EVGENTEXT").ToString)), enmTypedata.typeother)

                                    'nAmlo1Psn(iPsnCount).p224_psn_iddoc_date = setValueDefault(dr("EVGEN").ToString, enmTypedata.typeDate)
                                    'nAmlo1Data.p122_cnd_iddoc_date = setValueDefault(clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGENTEXT").ToString), enmTypedata.typeDate)

                                Catch ex As Exception

                                End Try
                                Try
                                    nAmlo1Psn(iPsnCount).p225_psn_iddoc_expire = setValueDefault(If(dr("EVEXPIRETEXT").ToString = "-", dr("EVEXPIRETEXT").ToString, clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dr("EVEXPIRETEXT").ToString)), enmTypedata.typeother)
                                Catch ex As Exception

                                End Try

                            Next

                        End If
                        'iAllRec - 1
                        If Not (AmloXML.AddAMLO1node(path, nAmlo1Data, iPsnCount, nAmlo1Psn, CountChkToGen, CountChk)) Then
                            Exit Sub
                        End If

                    End If

                Next
                DownloadFile("C:\ExportXML\AMLO101.xml", "AMLO101.xml", True)
                ClearObj()

                'MapPath("AMLO101.xml")



            ElseIf (ddlRrptType.SelectedValue = "M52") Then
                'Dim qryAmlo = From tblAmlo In AMLO102Entity.AMLO102 Join tblAmloDetail In AMLO102Entity.AMLODETAIL102 On tblAmlo.RPTSEQ Equals tblAmloDetail.RPTSEQ
                '             Where tblAmloDetail.RPTIDX = 1 And tblAmlo.ISCOMPLETE = 1 _
                '             Order By tblAmlo.RPTSEQ Descending _
                '             Select tblAmlo.RPTYEAR, tblAmlo.RPTREV, tblAmlo.ISSEND, tblAmlo.RPTNO, tblAmloDetail.FIRSTNAME, tblAmloDetail.LASTNAME, tblAmlo.BRANCHCODE, tblAmlo.TRANSDATE, tblAmlo.TRANSTYPE, tblAmlo.SIGNTYPE, tblAmlo.UPDATEBY, tblAmlo.SAVEDATE, tblAmlo.RPTSEQ, tblAmlo.ISCOMPLETE, tblAmlo.ISDELETE, tblAmlo.ISCONFIRM

                Dim qryAmlo = From tblAmlo In AMLO102Entity.AMLO102 Join tblAmloDetail In AMLO102Entity.AMLODETAIL102 On tblAmlo.RPTSEQ Equals tblAmloDetail.RPTSEQ
                          Where tblAmloDetail.RPTIDX = 1 And tblAmlo.ISCOMPLETE = 1 _
                          Order By tblAmlo.RPTSEQ Descending, tblAmlo.DEPTCODE Descending, tblAmlo.BRANCHCODE Descending _
                          Select tblAmlo.RPTSEQ, tblAmlo.GHBCODE, tblAmlo.RPTTYPE, tblAmlo.RPTYEAR, tblAmlo.RPTNO, _
                          tblAmlo.RPTREV, tblAmlo.BRANCHCODE, tblAmlo.SOL_ID, tblAmlo.DEPTCODE, tblAmlo.RPTDATE, tblAmlo.TRANSDATE, _
                          tblAmlo.TRANSTYPE, tblAmlo.TRANSTYPEOTHER, tblAmlo.ASSETTYPE, tblAmlo.ASSETTYPEOTHER, _
                          tblAmlo.ASSETTYPEDETAIL, tblAmlo.TRANSAMT, tblAmlo.TRANSAMTCHAR, tblAmlo.CURRENCY, tblAmlo.ACCOUNTNO, _
                          tblAmlo.ACCOUNTNAME, tblAmlo.ACCPREFIX, tblAmlo.ACCFIRSTNAME, tblAmlo.ACCMIDNAME, tblAmlo.ACCLASTNAME, _
                          tblAmlo.ACCOUNTREF, tblAmlo.ACCOUNTREFNAME, tblAmlo.REFPREFIX, tblAmlo.REFFIRSTNAME, tblAmlo.REFMIDNAME, _
                          tblAmlo.REFLASTNAME, tblAmlo.REFERENCETYPE, tblAmlo.REFERENCETYPEOTHER, tblAmlo.GAINPREFIX, _
                          tblAmlo.GAINFIRSTNAME, tblAmlo.GAINMIDNAME, tblAmlo.GAINLASTNAME, tblAmlo.OBJBIZ, tblAmlo.OBJBIZOTHER, _
                          tblAmlo.SAVETYPE, tblAmlo.SAVEDATE, tblAmlo.FINANORGCODE, tblAmlo.SIGNTYPE, tblAmlo.ISSEND, _
                          tblAmloDetail.RPTIDX, tblAmloDetail.RPTIDXTYPE, tblAmloDetail.UNIQUETYPE, tblAmloDetail.UNIQUEID, _
                          tblAmloDetail.PREFIX, tblAmloDetail.FIRSTNAME, tblAmloDetail.MIDNAME, tblAmloDetail.LASTNAME, _
                          tblAmloDetail.FORMALADDR, tblAmloDetail.FORMALPHONE, tblAmloDetail.FORMALFAX, tblAmloDetail.OCCUPATIONTYPE, _
                          tblAmloDetail.OCCUPATIONTYPEOTHER, tblAmloDetail.WORKADDR, tblAmloDetail.WORKPHONE, tblAmloDetail.WORKFAX, _
                          tblAmloDetail.BIZTYPE, tblAmloDetail.BIZTYPEOTHER, tblAmloDetail.CONTACTADDR, tblAmloDetail.CONTACTPHONE, _
                          tblAmloDetail.CONTACTFAX, tblAmloDetail.CUSTIDTYPE, tblAmloDetail.CUSTIDTYPEOTHER, tblAmloDetail.CUSTID, _
                          tblAmloDetail.EVBY, tblAmloDetail.EVGEN, tblAmloDetail.EVGENTEXT, tblAmloDetail.EVEXPIRE, tblAmloDetail.EVEXPIRETEXT

                If Not (ddlDepartment.SelectedValue = "0") Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = ddlDepartment.SelectedValue)
                End If

                If Not (ddlBranch.SelectedIndex = 0) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.BRANCHCODE.Contains(ddlBranch.SelectedValue))
                End If

                'If Not (txtYear.Text = "") Then
                '    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTYEAR.Value = CInt(txtYear.Text))
                'End If
                If Not (txtNo.Text = "") Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTNO.Contains(txtNo.Text))
                End If

                If rdolTypeDate.SelectedValue = 1 Then
                    If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                        Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                        Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.TRANSDATE >= strStartDate And tbl_Amlo.TRANSDATE <= strEndDate)
                    End If
                Else

                    If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                        Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                        Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SAVEDATE >= strStartDate And tbl_Amlo.SAVEDATE <= strEndDate)
                    End If
                End If

                If Not (rdolSignType.SelectedValue = 0) Then
                    If (rdolSignType.SelectedValue = 1) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 1)
                    ElseIf (rdolSignType.SelectedValue = 2) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 0)
                    End If

                End If

                If Not (rdolIsSend.SelectedValue = 0) Then
                    If (rdolIsSend.SelectedValue = 1) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 1)
                    ElseIf (rdolIsSend.SelectedValue = 2) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 0)
                    End If

                End If

                'dsAmloXML.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
                'If (qryAmlo.Count = 0) Then
                '    lblMsg.Visible = True
                'Else
                '    lblMsg.Visible = False

                'End If
                dsAmloXML.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
                If (qryAmlo.Count = 0) Then
                    lblMsg.Visible = True
                Else
                    lblMsg.Visible = False
                End If

                Dim AmloXML As New clsAmloXML
                Dim path As String
                Dim iCount, iAllRec As Integer
                Dim CountChk As Integer = 0
                Dim CountChkToGen As Integer = 0
                Dim nAmlo2Data As clsAmlo2Data = New clsAmlo2Data
                Dim nAmlo2Psn(9) As clsAmlo2Psn
                iAllRec = dsAmloXML.Tables("Amlo").Rows.Count

                For iCount = 0 To iAllRec - 1
                    Dim chkConfirmGen As CheckBox = CType(grdAmlo.Rows(iCount).FindControl("CheckBox1"), CheckBox)
                    If (chkConfirmGen.Checked) Then
                        CountChk += 1
                    End If
                Next

                path = "C:\EXPORTXML\AMLO102.xml"
                FileCopy("C:\EXPORTXML\Template\AMLO102.xml", "C:\EXPORTXML\AMLO102.xml")
                For iCount = 0 To iAllRec - 1

                    Dim chkConfirmGen As CheckBox = CType(grdAmlo.Rows(iCount).FindControl("CheckBox1"), CheckBox)
                    If (chkConfirmGen.Checked) Then
                        CountChkToGen += 1


                        'ชนิดของรายงาน 1-01
                        nAmlo2Data.p01_type = "1-02"
                        'เลขที่รายงาน 

                        Dim strRptNo As String = ""

                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("GHBCODE").ToString
                        'strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("DEPTCODE").ToString
                        'strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("BRANCHCODE").ToString.Substring(0, 3)
                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("SOL_ID").ToString
                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTYEAR").ToString
                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTNO").ToString

                        nAmlo2Data.p02_reportid = strRptNo 'dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTNO").ToString
                        'ครั้งที่แก้ไข/เพิ่มเติม  -ถ้าไม่ระบุจะถือเป็นฉบับแรก
                        nAmlo2Data.p03_revision = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTREV").ToString, enmTypedata.typeString)
                        'วันที่รายงาน -วันที่/เดือน/ปี พ.ศ.(4หลัก)
                        nAmlo2Data.p04_date = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTDATE").ToString, enmTypedata.typeDate)
                        '=================================
                        'ส่วนที่ 1. ผู้ทำธุรกรรม 
                        nAmlo2Data.p101_cnd_name_prefix = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("PREFIX").ToString, enmTypedata.typeString)
                        nAmlo2Data.p102_cnd_name_first = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FIRSTNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p103_cnd_name_middle = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("MIDNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p104_cnd_name_last = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("LASTNAME").ToString, enmTypedata.typeString)

                        nAmlo2Data.p105_cnd_id_no = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("UNIQUEID").ToString, enmTypedata.typeString)
                        nAmlo2Data.p106_cnd_id_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("UNIQUETYPE").ToString, enmTypedata.typeString)

                        nAmlo2Data.p107_cnd_location_addr = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FORMALADDR").ToString, enmTypedata.typeString)
                        nAmlo2Data.p108_cnd_location_phone = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FORMALPHONE").ToString, enmTypedata.typeString)
                        nAmlo2Data.p109_cnd_location_fax = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FORMALFAX").ToString, enmTypedata.typeString)


                        'ระบุอาชีพ 1-เจ้าของกิจการ, 2-ข้าราชการ, 3-รับจ้าง, 4-พนักงานรัฐวิสาหกิจ, 5-อาชีพอิสระ, 6-นิสิต นักศึกษา, 99-อื่นๆ
                        nAmlo2Data.p110_cnd_office_occ = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OCCUPATIONTYPE").ToString, enmTypedata.typeString)
                        nAmlo2Data.p111_cnd_office_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OCCUPATIONTYPEOTHER").ToString, enmTypedata.typeString)
                        nAmlo2Data.p112_cnd_office_org = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("WORKADDR").ToString, enmTypedata.typeString)
                        nAmlo2Data.p113_cnd_office_phone = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("WORKPHONE").ToString, enmTypedata.typeString)
                        nAmlo2Data.p114_cnd_office_fax = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("WORKFAX").ToString, enmTypedata.typeString)


                        nAmlo2Data.p115_cnd_contact_addr = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CONTACTADDR").ToString, enmTypedata.typeString)
                        nAmlo2Data.p116_cnd_contact_phone = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CONTACTPHONE").ToString, enmTypedata.typeString)
                        nAmlo2Data.p117_cnd_contact_fax = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CONTACTFAX").ToString, enmTypedata.typeString)


                        'ชนิดหลักฐาน 1-บัตรประจำตัวประชาชน (ต้องเป็นตัวเลขและมีขนาด 13 หลักเท่านั้น), 2-บัตรประจำตัวผู้เสียภาษีอากร (ต้องเป็นตัวเลขและมีขนาด 10 หลักเท่านั้น), 3-หนังสือเดินทาง, 4-ใบสำคัญคนต่างด้าว,
                        ' 5- หนังสือรับรองข้อความในทะเบียน, 6 - บัตรที่ออกโดยองค์กรรัฐ, 7 - หนังสือสุทธิสำหรับพระภิกษุ, 8 -บัตรประจำตัวชนกลุ่มน้อย, 99 - อื่นๆ 
                        nAmlo2Data.p118_cnd_iddoc_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CUSTIDTYPE").ToString, enmTypedata.typeString)
                        nAmlo2Data.p119_cnd_iddoc_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CUSTIDTYPEOTHER").ToString, enmTypedata.typeString)
                        nAmlo2Data.p120_cnd_iddoc_no = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CUSTID").ToString, enmTypedata.typeString)
                        nAmlo2Data.p121_cnd_iddoc_by = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVBY").ToString, enmTypedata.typeString)

                        Try
                            'nAmlo2Data.p122_cnd_iddoc_date = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGEN").ToString, enmTypedata.typeDate)
                            nAmlo2Data.p122_cnd_iddoc_date = setValueDefault(If(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGENTEXT").ToString = "-", dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGENTEXT").ToString, clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGENTEXT").ToString)), enmTypedata.typeother)
                        Catch ex As Exception

                        End Try

                        Try
                            ' nAmlo2Data.p123_cnd_iddoc_expire = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRE").ToString, enmTypedata.typeDate)
                            nAmlo2Data.p123_cnd_iddoc_expire = setValueDefault(If(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRETEXT").ToString = "-", dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRETEXT").ToString, clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRETEXT").ToString)), enmTypedata.typeother)

                        Catch ex As Exception

                        End Try

                        '==================================
                        'ส่วนที่ 3. ข้อเท็จจริงเกี่ยวกับธุรกรรม

                        nAmlo2Data.p301_tsc_date = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSDATE").ToString, enmTypedata.typeDate)
                        nAmlo2Data.p302_tsc_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSTYPE").ToString, enmTypedata.typeString)
                        nAmlo2Data.p303_tsc_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSTYPEOTHER").ToString, enmTypedata.typeString)
                        nAmlo2Data.p304_tsc_asset_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ASSETTYPE").ToString, enmTypedata.typeString)

                        nAmlo2Data.p305_tsc_asset_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ASSETTYPEOTHER").ToString, enmTypedata.typeString)
                        nAmlo2Data.p306_tsc_asset_detail = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ASSETTYPEDETAIL").ToString, enmTypedata.typeString)
                        nAmlo2Data.p307_tsc_asset_amt = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSAMT").ToString, enmTypedata.typeString)
                        nAmlo2Data.p308_tsc_acc_accno = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCOUNTNO").ToString, enmTypedata.typeString)
                        nAmlo2Data.p309_tsc_acc_aname = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCOUNTNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p310_tsc_acc_name_prefix = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCPREFIX").ToString, enmTypedata.typeString)
                        nAmlo2Data.p311_tsc_acc_name_first = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCFIRSTNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p312_tsc_acc_name_middle = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCMIDNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p313_tsc_acc_name_last = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCLASTNAME").ToString, enmTypedata.typeString)

                        nAmlo2Data.p314_tsc_racc_accno = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCOUNTREF").ToString, enmTypedata.typeString)
                        nAmlo2Data.p315_tsc_racc_aname = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCOUNTREFNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p316_tsc_racc_name_prefix = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFPREFIX").ToString, enmTypedata.typeString)
                        nAmlo2Data.p317_tsc_racc_name_first = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFFIRSTNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p318_tsc_racc_name_middle = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFMIDNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p319_tsc_racc_name_last = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFLASTNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p320_tsc_racc_relation_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFERENCETYPE").ToString, enmTypedata.typeString)
                        nAmlo2Data.p321_tsc_racc_relation_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFERENCETYPEOTHER").ToString, enmTypedata.typeString)
                        nAmlo2Data.p322_tsc_name_prefix = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINPREFIX").ToString, enmTypedata.typeString)
                        nAmlo2Data.p323_tsc_name_first = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINFIRSTNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p324_tsc_name_middle = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINMIDNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p325_tsc_name_last = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINLASTNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p326_tsc_obj_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OBJBIZ").ToString, enmTypedata.typeString)
                        nAmlo2Data.p327_tsc_obj_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OBJBIZOTHER").ToString, enmTypedata.typeString)
                        nAmlo2Data.p401_record_writer = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("SAVETYPE").ToString, enmTypedata.typeString)
                        nAmlo2Data.p402_record_sign = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("SIGNTYPE").ToString, enmTypedata.typeString)
                        nAmlo2Data.p403_record_date = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("SAVEDATE").ToString, enmTypedata.typeDate)



                        Dim mRptSeq As Integer = Convert.ToInt32(dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTSEQ").ToString)
                        Dim qryAmloDetailCount = From tblAmloDetail In AMLO102Entity.AMLODETAIL102
                         Where tblAmloDetail.RPTSEQ = mRptSeq And tblAmloDetail.RPTIDX > 1
                         Group By tblAmloDetail.RPTSEQ Into MaxDetail = Max(tblAmloDetail.RPTIDX)

                        dsAmloXMLCount = New DataSet()
                        '    NewObjAmloDetail()
                        dsAmloXMLCount.Tables.Add(clsDataTable.LINQToDataTable(qryAmloDetailCount, "AmloCount").Copy)


                        Dim iPsnCount As Integer = 0
                        If dsAmloXMLCount.Tables("AmloCount").Rows.Count > 0 Then
                            Dim mMaxRptDetail As Integer = Convert.ToInt32(dsAmloXMLCount.Tables("AmloCount").Rows(0).Item("MaxDetail").ToString)
                            Dim qryAmloDetail = From tblAmloDetail In AMLO102Entity.AMLODETAIL102
                            Where tblAmloDetail.RPTSEQ = mRptSeq And tblAmloDetail.RPTIDX > 1
                            Order By tblAmloDetail.RPTIDX
                             Select tblAmloDetail.EVBY, tblAmloDetail.EVGEN, tblAmloDetail.EVEXPIRE, tblAmloDetail.CUSTIDTYPE, tblAmloDetail.CUSTIDTYPEOTHER, _
                                tblAmloDetail.CUSTID, tblAmloDetail.OCCUPATIONTYPE, tblAmloDetail.OCCUPATIONTYPEOTHER, tblAmloDetail.CONTACTADDR, _
                                 tblAmloDetail.CONTACTPHONE, tblAmloDetail.CONTACTFAX, tblAmloDetail.FORMALADDR, tblAmloDetail.FORMALPHONE, tblAmloDetail.BIZTYPE, _
                            tblAmloDetail.FORMALFAX, tblAmloDetail.WORKADDR, tblAmloDetail.WORKPHONE, tblAmloDetail.WORKFAX, tblAmloDetail.RPTIDXTYPE, _
                            tblAmloDetail.PREFIX, tblAmloDetail.FIRSTNAME, tblAmloDetail.MIDNAME, tblAmloDetail.LASTNAME, tblAmloDetail.UNIQUETYPE, tblAmloDetail.UNIQUEID

                            dsAmloXMLDetail = New DataSet()
                            dsAmloXMLDetail.Tables.Add(clsDataTable.LINQToDataTable(qryAmloDetail, "AmloDetail").Copy)


                            For Each dr As DataRow In dsAmloXMLDetail.Tables("AmloDetail").Rows
                                iPsnCount = iPsnCount + 1
                                nAmlo2Psn(iPsnCount) = New clsAmlo2Psn(iPsnCount)


                                nAmlo2Psn(iPsnCount).p201_psn_relation = setValueDefault(dr("RPTIDXTYPE").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p202_psn_name_prefix = setValueDefault(dr("PREFIX").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p203_psn_name_first = setValueDefault(dr("FIRSTNAME").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p204_psn_name_middle = setValueDefault(dr("MIDNAME").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p205_psn_name_last = setValueDefault(dr("LASTNAME").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p206_psn_id_no = setValueDefault(dr("UNIQUEID").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p207_psn_id_type = setValueDefault(dr("UNIQUETYPE").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p208_psn_location_addr = setValueDefault(dr("FORMALADDR").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p209_psn_location_phone = setValueDefault(dr("FORMALPHONE").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p210_psn_location_fax = setValueDefault(dr("FORMALFAX").ToString, enmTypedata.typeString)


                                'ระบุอาชีพ 1-เจ้าของกิจการ, 2-ข้าราชการ, 3-รับจ้าง, 4-พนักงานรัฐวิสาหกิจ, 5-อาชีพอิสระ, 6-นิสิต นักศึกษา, 99-อื่นๆ
                                nAmlo2Psn(iPsnCount).p211_psn_office_occ = setValueDefault(dr("OCCUPATIONTYPE").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p212_psn_office_org = setValueDefault(dr("WORKADDR").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p213_psn_office_phone = setValueDefault(dr("WORKPHONE").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p214_psn_office_fax = setValueDefault(dr("WORKFAX").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p215_psn_office_type = setValueDefault(dr("BIZTYPE").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p216_psn_office_desc = setValueDefault(dr("OCCUPATIONTYPEOTHER").ToString, enmTypedata.typeString)

                                nAmlo2Psn(iPsnCount).p217_psn_contact_addr = setValueDefault(dr("CONTACTADDR").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p218_psn_contact_phone = setValueDefault(dr("CONTACTPHONE").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p219_psn_contact_fax = setValueDefault(dr("CONTACTFAX").ToString, enmTypedata.typeString)

                                'ชนิดหลักฐาน 1-บัตรประจำตัวประชาชน (ต้องเป็นตัวเลขและมีขนาด 13 หลักเท่านั้น), 2-บัตรประจำตัวผู้เสียภาษีอากร (ต้องเป็นตัวเลขและมีขนาด 10 หลักเท่านั้น), 3-หนังสือเดินทาง, 4-ใบสำคัญคนต่างด้าว,
                                ' 5- หนังสือรับรองข้อความในทะเบียน, 6 - บัตรที่ออกโดยองค์กรรัฐ, 7 - หนังสือสุทธิสำหรับพระภิกษุ, 8 -บัตรประจำตัวชนกลุ่มน้อย, 99 - อื่นๆ 

                                nAmlo2Psn(iPsnCount).p220_psn_iddoc_type = setValueDefault(dr("CUSTIDTYPE").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p221_psn_iddoc_desc = setValueDefault(dr("CUSTIDTYPEOTHER").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p222_psn_iddoc_no = setValueDefault(dr("CUSTID").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p223_psn_iddoc_by = setValueDefault(dr("EVBY").ToString, enmTypedata.typeString)

                                Try
                                    'nAmlo2Psn(iPsnCount).p224_psn_iddoc_date = setValueDefault(dr("EVGEN").ToString, enmTypedata.typeDate)
                                    nAmlo2Psn(iPsnCount).p224_psn_iddoc_date = setValueDefault(If(dr("EVGENTEXT").ToString = "-", dr("EVGENTEXT").ToString, clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dr("EVGENTEXT").ToString)), enmTypedata.typeother)

                                Catch ex As Exception

                                End Try
                                Try
                                    ' nAmlo2Psn(iPsnCount).p225_psn_iddoc_expire = setValueDefault(dr("EVEXPIRE").ToString, enmTypedata.typeDate)
                                    nAmlo2Psn(iPsnCount).p225_psn_iddoc_expire = setValueDefault(If(dr("EVEXPIRETEXT").ToString = "-", dr("EVEXPIRETEXT").ToString, clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dr("EVEXPIRETEXT").ToString)), enmTypedata.typeother)
                                Catch ex As Exception

                                End Try

                            Next
                        End If
                        If Not (AmloXML.AddAMLO2node(path, nAmlo2Data, iPsnCount, nAmlo2Psn, CountChkToGen, CountChk)) Then
                            Exit Sub
                        End If


                    End If
                Next

                DownloadFile("C:\ExportXML\AMLO102.xml", "AMLO102.xml", True)
                ClearObj()


            ElseIf (ddlRrptType.SelectedValue = "M53") Then
                Dim qryAmlo = From tblAmlo In AMLO103Entity.AMLO103 Join tblAmloDetail In AMLO103Entity.AMLODETAIL103 On tblAmlo.RPTSEQ Equals tblAmloDetail.RPTSEQ
                      Where tblAmloDetail.RPTIDX = 1 And tblAmlo.ISCOMPLETE = 1 _
                      Order By tblAmlo.RPTSEQ Descending, tblAmlo.DEPTCODE Descending, tblAmlo.BRANCHCODE Descending _
                      Select tblAmlo.RPTSEQ, tblAmlo.GHBCODE, tblAmlo.RPTTYPE, tblAmlo.RPTYEAR, tblAmlo.RPTNO, _
                        tblAmlo.RPTREV, tblAmlo.BRANCHCODE, tblAmlo.SOL_ID, tblAmlo.DEPTCODE, tblAmlo.RPTDATE, tblAmlo.TRANSDATE, _
                        tblAmlo.TRANSTYPE, tblAmlo.TRANSTYPEOTHER, tblAmlo.TRANSAMT, tblAmlo.TRANSAMTCHAR, _
                        tblAmlo.CURRENCY, tblAmlo.ACCOUNTNO, tblAmlo.ACCOUNTNAME, tblAmlo.ACCPREFIX, tblAmlo.ACCFIRSTNAME, _
                        tblAmlo.ACCMIDNAME, tblAmlo.ACCLASTNAME, tblAmlo.ACCOUNTREF, tblAmlo.ACCOUNTREFNAME, tblAmlo.REFPREFIX, _
                        tblAmlo.REFFIRSTNAME, tblAmlo.REFMIDNAME, tblAmlo.REFLASTNAME, tblAmlo.REFERENCETYPE, tblAmlo.REFERENCETYPEOTHER, _
                        tblAmlo.GAINPREFIX, tblAmlo.GAINFIRSTNAME, tblAmlo.GAINMIDNAME, tblAmlo.GAINLASTNAME, tblAmlo.OBJBIZ, _
                        tblAmlo.OBJBIZOTHER, tblAmlo.SAVETYPE, tblAmlo.SAVEDATE, tblAmlo.FINANORGCODE, tblAmlo.RESON, tblAmlo.ISSEND, tblAmlo.DOCREF, _
                        tblAmloDetail.RPTIDX, tblAmloDetail.RPTIDXTYPE, tblAmloDetail.UNIQUETYPE, tblAmloDetail.UNIQUEID, _
                        tblAmloDetail.PREFIX, tblAmloDetail.FIRSTNAME, tblAmloDetail.MIDNAME, tblAmloDetail.LASTNAME, _
                        tblAmloDetail.FORMALADDR, tblAmloDetail.FORMALPHONE, tblAmloDetail.FORMALFAX, tblAmloDetail.OCCUPATIONTYPE, _
                        tblAmloDetail.OCCUPATIONTYPEOTHER, tblAmloDetail.WORKADDR, tblAmloDetail.WORKPHONE, tblAmloDetail.WORKFAX, _
                        tblAmloDetail.BIZTYPE, tblAmloDetail.BIZTYPEOTHER, tblAmloDetail.CONTACTADDR, tblAmloDetail.CONTACTPHONE, _
                        tblAmloDetail.CONTACTFAX, tblAmloDetail.CUSTIDTYPE, tblAmloDetail.CUSTIDTYPEOTHER, tblAmloDetail.CUSTID, _
                        tblAmloDetail.EVBY, tblAmloDetail.EVGEN, tblAmloDetail.EVGENTEXT, tblAmloDetail.EVEXPIRE, tblAmloDetail.EVEXPIRETEXT

                If Not (ddlDepartment.SelectedValue = "0") Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = ddlDepartment.SelectedValue)
                End If

                If Not (ddlBranch.SelectedIndex = 0) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.BRANCHCODE.Contains(ddlBranch.SelectedValue))
                End If

                'If Not (txtYear.Text = "") Then
                '    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTYEAR.Value = CInt(txtYear.Text))
                'End If
                If Not (txtNo.Text = "") Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTNO.Contains(txtNo.Text))
                End If

                If rdolTypeDate.SelectedValue = 1 Then
                    If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                        Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                        Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.TRANSDATE >= strStartDate And tbl_Amlo.TRANSDATE <= strEndDate)
                    End If
                Else

                    If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                        Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                        Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SAVEDATE >= strStartDate And tbl_Amlo.SAVEDATE <= strEndDate)
                    End If
                End If

                'If Not (rdolSignType.SelectedValue = 0) Then
                '    If (rdolSignType.SelectedValue = 1) Then
                '        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 1)
                '    ElseIf (rdolSignType.SelectedValue = 2) Then
                '        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 0)
                '    End If

                'End If

                If Not (rdolIsSend.SelectedValue = 0) Then
                    If (rdolIsSend.SelectedValue = 1) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 1)
                    ElseIf (rdolIsSend.SelectedValue = 2) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 0)
                    End If

                End If

                'dsAmloXML.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
                'If (qryAmlo.Count = 0) Then
                '    lblMsg.Visible = True
                'Else
                '    lblMsg.Visible = False

                'End If
                dsAmloXML.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
                If (qryAmlo.Count = 0) Then
                    lblMsg.Visible = True
                Else
                    lblMsg.Visible = False
                End If

                Dim AmloXML As New clsAmloXML
                Dim path As String
                Dim iCount, iAllRec As Integer
                Dim CountChk As Integer = 0
                Dim CountChkToGen As Integer = 0
                Dim nAmlo3Data As clsAmlo3Data = New clsAmlo3Data
                Dim nAmlo3Psn(9) As clsAmlo3Psn
                iAllRec = dsAmloXML.Tables("Amlo").Rows.Count

                For iCount = 0 To iAllRec - 1
                    Dim chkConfirmGen As CheckBox = CType(grdAmlo.Rows(iCount).FindControl("CheckBox1"), CheckBox)
                    If (chkConfirmGen.Checked) Then
                        CountChk += 1
                    End If
                Next

                path = "C:\EXPORTXML\AMLO103.xml"
                FileCopy("C:\EXPORTXML\Template\AMLO103.xml", "C:\EXPORTXML\AMLO103.xml")
                For iCount = 0 To iAllRec - 1

                    Dim chkConfirmGen As CheckBox = CType(grdAmlo.Rows(iCount).FindControl("CheckBox1"), CheckBox)
                    If (chkConfirmGen.Checked) Then
                        CountChkToGen += 1



                        'ชนิดของรายงาน 1-01
                        nAmlo3Data.p01_type = "1-03"
                        'เลขที่รายงาน 
                        Dim strRptNo As String = ""

                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("GHBCODE").ToString
                        'strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("DEPTCODE").ToString
                        'strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("BRANCHCODE").ToString.Substring(0, 3)
                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("SOL_ID").ToString
                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTYEAR").ToString
                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTNO").ToString

                        nAmlo3Data.p02_reportid = strRptNo
                        'ครั้งที่แก้ไข/เพิ่มเติม  -ถ้าไม่ระบุจะถือเป็นฉบับแรก
                        nAmlo3Data.p03_revision = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTREV").ToString, enmTypedata.typeString)
                        'วันที่รายงาน -วันที่/เดือน/ปี พ.ศ.(4หลัก)
                        nAmlo3Data.p04_date = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTDATE").ToString, enmTypedata.typeDate)
                        'ประกอบรายงานเลขที่
                        nAmlo3Data.p05_refer = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("DOCREF").ToString, enmTypedata.typeString)
                        '=================================
                        'ส่วนที่ 1. ผู้ทำธุรกรรม 
                        nAmlo3Data.p101_cnd_name_prefix = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("PREFIX").ToString, enmTypedata.typeString)
                        nAmlo3Data.p102_cnd_name_first = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FIRSTNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p103_cnd_name_middle = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("MIDNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p104_cnd_name_last = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("LASTNAME").ToString, enmTypedata.typeString)

                        nAmlo3Data.p105_cnd_id_no = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("UNIQUEID").ToString, enmTypedata.typeString)
                        nAmlo3Data.p106_cnd_id_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("UNIQUETYPE").ToString, enmTypedata.typeString)

                        nAmlo3Data.p107_cnd_location_addr = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FORMALADDR").ToString, enmTypedata.typeString)
                        nAmlo3Data.p108_cnd_location_phone = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FORMALPHONE").ToString, enmTypedata.typeString)
                        nAmlo3Data.p109_cnd_location_fax = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FORMALFAX").ToString, enmTypedata.typeString)


                        'ระบุอาชีพ 1-เจ้าของกิจการ, 2-ข้าราชการ, 3-รับจ้าง, 4-พนักงานรัฐวิสาหกิจ, 5-อาชีพอิสระ, 6-นิสิต นักศึกษา, 99-อื่นๆ
                        nAmlo3Data.p110_cnd_office_occ = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OCCUPATIONTYPE").ToString, enmTypedata.typeString)
                        nAmlo3Data.p111_cnd_office_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OCCUPATIONTYPEOTHER").ToString, enmTypedata.typeString)
                        nAmlo3Data.p112_cnd_office_org = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("WORKADDR").ToString, enmTypedata.typeString)
                        nAmlo3Data.p113_cnd_office_phone = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("WORKPHONE").ToString, enmTypedata.typeString)
                        nAmlo3Data.p114_cnd_office_fax = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("WORKFAX").ToString, enmTypedata.typeString)


                        nAmlo3Data.p115_cnd_contact_addr = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CONTACTADDR").ToString, enmTypedata.typeString)
                        nAmlo3Data.p116_cnd_contact_phone = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CONTACTPHONE").ToString, enmTypedata.typeString)
                        nAmlo3Data.p117_cnd_contact_fax = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CONTACTFAX").ToString, enmTypedata.typeString)


                        'ชนิดหลักฐาน 1-บัตรประจำตัวประชาชน (ต้องเป็นตัวเลขและมีขนาด 13 หลักเท่านั้น), 2-บัตรประจำตัวผู้เสียภาษีอากร (ต้องเป็นตัวเลขและมีขนาด 10 หลักเท่านั้น), 3-หนังสือเดินทาง, 4-ใบสำคัญคนต่างด้าว,
                        ' 5- หนังสือรับรองข้อความในทะเบียน, 6 - บัตรที่ออกโดยองค์กรรัฐ, 7 - หนังสือสุทธิสำหรับพระภิกษุ, 8 -บัตรประจำตัวชนกลุ่มน้อย, 99 - อื่นๆ 
                        nAmlo3Data.p118_cnd_iddoc_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CUSTIDTYPE").ToString, enmTypedata.typeString)
                        nAmlo3Data.p119_cnd_iddoc_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CUSTIDTYPEOTHER").ToString, enmTypedata.typeString)
                        nAmlo3Data.p120_cnd_iddoc_no = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CUSTID").ToString, enmTypedata.typeString)
                        nAmlo3Data.p121_cnd_iddoc_by = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVBY").ToString, enmTypedata.typeString)

                        Try
                            'nAmlo3Data.p122_cnd_iddoc_date = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGEN").ToString, enmTypedata.typeDate)
                            nAmlo3Data.p122_cnd_iddoc_date = setValueDefault(If(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGENTEXT").ToString = "-", dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGENTEXT").ToString,
                                                                        clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGENTEXT").ToString)), enmTypedata.typeother)

                        Catch ex As Exception

                        End Try
                        Try
                            ' nAmlo3Data.p123_cnd_iddoc_expire = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRE").ToString, enmTypedata.typeDate)
                            nAmlo3Data.p123_cnd_iddoc_expire = setValueDefault(If(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRETEXT").ToString = "-", dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRETEXT").ToString,
                                                                                  clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRETEXT").ToString)), enmTypedata.typeother)

                        Catch ex As Exception

                        End Try

                        'ส่วนที่ 3. ข้อเท็จจริงเกี่ยวกับธุรกรรม
                        nAmlo3Data.p301_tsc_date = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSDATE").ToString, enmTypedata.typeString)
                        nAmlo3Data.p302_tsc_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSTYPE").ToString, enmTypedata.typeString)
                        nAmlo3Data.p303_tsc_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSTYPEOTHER").ToString, enmTypedata.typeString)

                        nAmlo3Data.p304_tsc_cur = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CURRENCY").ToString, enmTypedata.typeString)
                        nAmlo3Data.p305_tsc_amt = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSAMT").ToString, enmTypedata.typeString)
                        nAmlo3Data.p306_tsc_acc_accno = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCOUNTNO").ToString, enmTypedata.typeString)
                        nAmlo3Data.p307_tsc_acc_aname = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCOUNTNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p308_tsc_acc_name_prefix = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCPREFIX").ToString, enmTypedata.typeString)
                        nAmlo3Data.p309_tsc_acc_name_first = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCFIRSTNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p310_tsc_acc_name_middle = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCMIDNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p311_tsc_acc_name_last = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCLASTNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p312_tsc_racc_accno = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCOUNTREF").ToString, enmTypedata.typeString)
                        nAmlo3Data.p313_tsc_racc_aname = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCOUNTREFNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p314_tsc_racc_name_prefix = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFPREFIX").ToString, enmTypedata.typeString)
                        nAmlo3Data.p315_tsc_racc_name_first = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFFIRSTNAME").ToString, enmTypedata.typeString)

                        nAmlo3Data.p316_tsc_racc_name_middle = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFMIDNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p317_tsc_racc_name_last = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFLASTNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p318_tsc_racc_relation_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFERENCETYPE").ToString, enmTypedata.typeString)
                        nAmlo3Data.p319_tsc_racc_relation_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFERENCETYPEOTHER").ToString, enmTypedata.typeString)
                        nAmlo3Data.p320_tsc_name_prefix = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINPREFIX").ToString, enmTypedata.typeString)
                        nAmlo3Data.p321_tsc_name_first = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINFIRSTNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p322_tsc_name_middle = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINMIDNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p323_tsc_name_last = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINLASTNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p324_tsc_obj_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OBJBIZ").ToString, enmTypedata.typeString)
                        nAmlo3Data.p325_tsc_obj_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OBJBIZOTHER").ToString, enmTypedata.typeString)
                        nAmlo3Data.p401_record_note = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("RESON").ToString, enmTypedata.typeString)




                        Dim mRptSeq As Integer = Convert.ToInt32(dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTSEQ").ToString)
                        Dim qryAmloDetailCount = From tblAmloDetail In AMLO103Entity.AMLODETAIL103
                         Where tblAmloDetail.RPTSEQ = mRptSeq And tblAmloDetail.RPTIDX > 1
                         Group By tblAmloDetail.RPTSEQ Into MaxDetail = Max(tblAmloDetail.RPTIDX)

                        dsAmloXMLCount = New DataSet()
                        '    NewObjAmloDetail()
                        dsAmloXMLCount.Tables.Add(clsDataTable.LINQToDataTable(qryAmloDetailCount, "AmloCount").Copy)


                        Dim iPsnCount As Integer = 0
                        If dsAmloXMLCount.Tables("AmloCount").Rows.Count > 0 Then
                            Dim mMaxRptDetail As Integer = Convert.ToInt32(dsAmloXMLCount.Tables("AmloCount").Rows(0).Item("MaxDetail").ToString)
                            Dim qryAmloDetail = From tblAmloDetail In AMLO103Entity.AMLODETAIL103
                            Where tblAmloDetail.RPTSEQ = mRptSeq And tblAmloDetail.RPTIDX > 1
                            Order By tblAmloDetail.RPTIDX
                             Select tblAmloDetail.EVBY, tblAmloDetail.EVGEN, tblAmloDetail.EVEXPIRE, tblAmloDetail.CUSTIDTYPE, tblAmloDetail.CUSTIDTYPEOTHER, _
                                tblAmloDetail.CUSTID, tblAmloDetail.OCCUPATIONTYPE, tblAmloDetail.OCCUPATIONTYPEOTHER, tblAmloDetail.CONTACTADDR, _
                                 tblAmloDetail.CONTACTPHONE, tblAmloDetail.CONTACTFAX, tblAmloDetail.FORMALADDR, tblAmloDetail.FORMALPHONE, tblAmloDetail.BIZTYPE, _
                            tblAmloDetail.FORMALFAX, tblAmloDetail.WORKADDR, tblAmloDetail.WORKPHONE, tblAmloDetail.WORKFAX, tblAmloDetail.RPTIDXTYPE, _
                            tblAmloDetail.PREFIX, tblAmloDetail.FIRSTNAME, tblAmloDetail.MIDNAME, tblAmloDetail.LASTNAME, tblAmloDetail.UNIQUETYPE, tblAmloDetail.UNIQUEID

                            dsAmloXMLDetail = New DataSet()
                            dsAmloXMLDetail.Tables.Add(clsDataTable.LINQToDataTable(qryAmloDetail, "AmloDetail").Copy)


                            For Each dr As DataRow In dsAmloXMLDetail.Tables("AmloDetail").Rows
                                iPsnCount = iPsnCount + 1
                                nAmlo3Psn(iPsnCount) = New clsAmlo3Psn(iPsnCount)


                                nAmlo3Psn(iPsnCount).p201_psn_relation = setValueDefault(dr("RPTIDXTYPE").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p202_psn_name_prefix = setValueDefault(dr("PREFIX").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p203_psn_name_first = setValueDefault(dr("FIRSTNAME").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p204_psn_name_middle = setValueDefault(dr("MIDNAME").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p205_psn_name_last = setValueDefault(dr("LASTNAME").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p206_psn_id_no = setValueDefault(dr("UNIQUEID").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p207_psn_id_type = setValueDefault(dr("UNIQUETYPE").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p208_psn_location_addr = setValueDefault(dr("FORMALADDR").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p209_psn_location_phone = setValueDefault(dr("FORMALPHONE").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p210_psn_location_fax = setValueDefault(dr("FORMALFAX").ToString, enmTypedata.typeString)


                                'ระบุอาชีพ 1-เจ้าของกิจการ, 2-ข้าราชการ, 3-รับจ้าง, 4-พนักงานรัฐวิสาหกิจ, 5-อาชีพอิสระ, 6-นิสิต นักศึกษา, 99-อื่นๆ
                                nAmlo3Psn(iPsnCount).p211_psn_office_occ = setValueDefault(dr("OCCUPATIONTYPE").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p212_psn_office_org = setValueDefault(dr("WORKADDR").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p213_psn_office_phone = setValueDefault(dr("WORKPHONE").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p214_psn_office_fax = setValueDefault(dr("WORKFAX").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p215_psn_office_type = setValueDefault(dr("BIZTYPE").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p216_psn_office_desc = setValueDefault(dr("OCCUPATIONTYPEOTHER").ToString, enmTypedata.typeString)

                                nAmlo3Psn(iPsnCount).p217_psn_contact_addr = setValueDefault(dr("CONTACTADDR").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p218_psn_contact_phone = setValueDefault(dr("CONTACTPHONE").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p219_psn_contact_fax = setValueDefault(dr("CONTACTFAX").ToString, enmTypedata.typeString)

                                'ชนิดหลักฐาน 1-บัตรประจำตัวประชาชน (ต้องเป็นตัวเลขและมีขนาด 13 หลักเท่านั้น), 2-บัตรประจำตัวผู้เสียภาษีอากร (ต้องเป็นตัวเลขและมีขนาด 10 หลักเท่านั้น), 3-หนังสือเดินทาง, 4-ใบสำคัญคนต่างด้าว,
                                ' 5- หนังสือรับรองข้อความในทะเบียน, 6 - บัตรที่ออกโดยองค์กรรัฐ, 7 - หนังสือสุทธิสำหรับพระภิกษุ, 8 -บัตรประจำตัวชนกลุ่มน้อย, 99 - อื่นๆ 

                                nAmlo3Psn(iPsnCount).p220_psn_iddoc_type = setValueDefault(dr("CUSTIDTYPE").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p221_psn_iddoc_desc = setValueDefault(dr("CUSTIDTYPEOTHER").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p222_psn_iddoc_no = setValueDefault(dr("CUSTID").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p223_psn_iddoc_by = setValueDefault(dr("EVBY").ToString, enmTypedata.typeString)
                                Try
                                    ' nAmlo3Psn(iPsnCount).p224_psn_iddoc_date = setValueDefault(dr("EVGEN").ToString, enmTypedata.typeDate)
                                    nAmlo3Psn(iPsnCount).p224_psn_iddoc_date = setValueDefault(If(dr("EVGENTEXT").ToString = "-", dr("EVGENTEXT").ToString, clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dr("EVGENTEXT").ToString)), enmTypedata.typeother)
                                Catch ex As Exception

                                End Try
                                Try
                                    'nAmlo3Psn(iPsnCount).p225_psn_iddoc_expire = setValueDefault(dr("EVEXPIRE").ToString, enmTypedata.typeDate)
                                    nAmlo3Psn(iPsnCount).p225_psn_iddoc_expire = setValueDefault(If(dr("EVEXPIRETEXT").ToString = "-", dr("EVEXPIRETEXT").ToString, clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dr("EVEXPIRETEXT").ToString)), enmTypedata.typeother)

                                Catch ex As Exception

                                End Try
                            Next
                        End If



                        If Not (AmloXML.AddAMLO3node(path, nAmlo3Data, iPsnCount, nAmlo3Psn, CountChkToGen, CountChk)) Then
                            Exit Sub
                        End If
                    End If
                Next

                DownloadFile("C:\ExportXML\AMLO103.xml", "AMLO103.xml", True)
                ClearObj()
            ElseIf (ddlRrptType.SelectedValue = "M57") Then
                Dim qryAmlo = From tblAmlo In AMLO1059Entity.AMLO1059 Join tblBranch In AMLO1059Entity.SBRANCHes _
                      On tblAmlo.INIT_SOL_ID Equals tblBranch.SOL_ID _
                     Group Join tblTran In AMLO1059Entity.MTSCTYPE_1_05_9 On tblAmlo.KEY_FLG Equals tblTran.TSCTYPEID
                      Into leftJoinAmlo = Group
                      From tblLeftAmlo In leftJoinAmlo.DefaultIfEmpty
                      Where tblAmlo.ISCONFIRM = 1
                     Order By tblAmlo.ID Ascending _
                Select tblAmlo.ID, tblAmlo.KEY_FLG, tblAmlo.P3_TRAN_ID, tblAmlo.P3_TRANS_TYPE, tblAmlo.P3_TRANS_DATE, tblAmlo.P3_TRAN_AMT, tblAmlo.INIT_SOL_ID, tblAmlo.P3_FORACID, tblAmlo.P3_CUST_NAME, tblAmlo.P2_CUST_FNAME, tblAmlo.P2_CUST_LNAME, _
                tblAmlo.P3_FORACID2, tblAmlo.P3_CUST_NAME2, tblAmlo.P2_CUST_FNAME2, tblAmlo.P2_CUST_LNAME2, TSCTYPEID = If(tblLeftAmlo.TSCTYPEID Is Nothing, "-", tblLeftAmlo.TSCTYPEID), _
                tblBranch.DEPTCODE, tblBranch.ZONECODE, tblBranch.BRANCHCODE, _
                tblLeftAmlo.TSCTYPENAME, tblAmlo.CREATEBY, tblAmlo.CREATEDATE, tblAmlo.UPDATEBY, tblAmlo.UPDATEDATE, _
                tblAmlo.ISCONFIRM, tblAmlo.ISSEND, tblAmlo.ISDELETE, tblAmlo.DCN_DOCUMENTNO

                If Not (ddlTransType.SelectedValue = "0") Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.KEY_FLG = ddlTransType.SelectedValue)
                End If

                If Not (ddlDepartment.SelectedValue = "0") Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = ddlDepartment.SelectedValue)
                End If

                If Not (ddlBranch.SelectedIndex = 0) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.BRANCHCODE.Contains(ddlBranch.SelectedValue))
                End If

                'If Not (txtYear.Text = "") Then
                '    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTYEAR.Value = CInt(txtYear.Text))
                'End If
                If Not (txtNo.Text = "") Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DCN_DOCUMENTNO.Contains(txtNo.Text))
                End If

                If rdolTypeDate.SelectedValue = 1 Then
                    If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                        Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                        Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.P3_TRANS_DATE >= strStartDate And tbl_Amlo.P3_TRANS_DATE <= strEndDate)
                    End If
                Else

                    If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                        Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                        Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.CREATEDATE >= strStartDate And tbl_Amlo.CREATEDATE <= strEndDate)
                    End If
                End If

                If Not (rdolIsSend.SelectedValue = 0) Then
                    If (rdolIsSend.SelectedValue = 1) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 1)
                    ElseIf (rdolIsSend.SelectedValue = 2) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 0)
                    End If

                End If

                dsAmloXML.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
                If (qryAmlo.Count = 0) Then
                    lblMsg.Visible = True
                    Exit Sub
                Else
                    lblMsg.Visible = False
                End If

                Dim AmloXML As New clsAmloXML
                Dim path As String
                Dim iCount, iAllRec As Integer
                Dim CountChk As Integer = 0
                iAllRec = dsAmloXML.Tables("Amlo").Rows.Count

                For iCount = 0 To iAllRec - 1
                    Dim chkConfirmGen As CheckBox = CType(grdAmlo1059.Rows(iCount).FindControl("CheckBox1"), CheckBox)
                    If (chkConfirmGen.Checked) Then
                        CountChk += 1
                    End If
                Next
                '////////////////////////
                path = "C:\EXPORTXML\AMLO1059.xml"
                FileCopy("C:\EXPORTXML\Template\AMLO1059.xml", "C:\EXPORTXML\AMLO1059.xml")

                iAllRec = dsAmloXML.Tables("Amlo").Rows.Count

                Dim idList = New List(Of Integer)
                For iCount = 0 To iAllRec - 1

                    Dim chkConfirmGen As CheckBox = CType(grdAmlo1059.Rows(iCount).FindControl("CheckBox1"), CheckBox)
                    If (chkConfirmGen.Checked) Then
                        CountChk += 1

                        Dim hdfId = CType(grdAmlo1059.Rows(iCount).FindControl("hdfId"), HiddenField)
                        idList.Add(CInt(hdfId.Value))
                    End If
                Next

                If Not (AmloXML.AddAMLO1059nodeWithAcid(path, idList, CountChk)) Then
                    Exit Sub
                End If

                DownloadFile("C:\ExportXML\AMLO1059.xml", "AMLO1059.xml", True)
                ClearObj()
                    End If



        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                              UpdatePanel1.GetType(), _
                                                              "clientScript", _
                                                              "alert('" + ex.Message.Replace("'", "").Replace("""", "") + "')", _
                                                              True)
        End Try
    End Sub

    Protected Sub btnGenXMLACID_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGenXMLACID.Click

        Dim dsAmloXML As DataSet
        clsFormatDate = New clsDateFormat

        Try
            ' ========================================
            If (ddlRrptType.SelectedValue = "M57") Then
                If (grdAmlo1059.Rows.Count = 0) Then
                    ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                             UpdatePanel1.GetType(), _
                                                             "clientScript", _
                                                             "alert('กรุณาเลือกข้อมูลที่ต้องการออกไฟล์ Xml ')", _
                                                             True)
                    Exit Sub
                End If
            End If

            clsDataTable = New clsLINQToDataTable
            clsFormatDate = New clsDateFormat
            Dim UserRight As clsSessionMenuRight
            UserRight = Session("clsMenuRight")

            dsAmloXML = New DataSet()

            NewObjAmlo()

            If (ddlRrptType.SelectedValue = "M57") Then
                Dim qryAmlo = From tblAmlo In AMLO1059Entity.AMLO1059 Join tblBranch In AMLO1059Entity.SBRANCHes _
                      On tblAmlo.INIT_SOL_ID Equals tblBranch.SOL_ID _
                     Group Join tblTran In AMLO1059Entity.MTSCTYPE_1_05_9 On tblAmlo.KEY_FLG Equals tblTran.TSCTYPEID
                      Into leftJoinAmlo = Group
                      From tblLeftAmlo In leftJoinAmlo.DefaultIfEmpty
                     Order By tblAmlo.ID Ascending _
                Select tblAmlo.ID, tblAmlo.KEY_FLG, tblAmlo.P3_TRAN_ID, tblAmlo.P3_TRANS_TYPE, tblAmlo.P3_TRANS_DATE, tblAmlo.P3_TRAN_AMT, tblAmlo.INIT_SOL_ID, tblAmlo.P3_FORACID, tblAmlo.P3_CUST_NAME, tblAmlo.P2_CUST_FNAME, tblAmlo.P2_CUST_LNAME, _
                tblAmlo.P3_FORACID2, tblAmlo.P3_CUST_NAME2, tblAmlo.P2_CUST_FNAME2, tblAmlo.P2_CUST_LNAME2, TSCTYPEID = If(tblLeftAmlo.TSCTYPEID Is Nothing, "-", tblLeftAmlo.TSCTYPEID), _
                tblBranch.DEPTCODE, tblBranch.ZONECODE, tblBranch.BRANCHCODE, _
                tblLeftAmlo.TSCTYPENAME, tblAmlo.CREATEBY, tblAmlo.CREATEDATE, tblAmlo.UPDATEBY, tblAmlo.UPDATEDATE, _
                tblAmlo.ISCONFIRM, tblAmlo.ISSEND, tblAmlo.ISDELETE, tblAmlo.DCN_DOCUMENTNO

                If Not (ddlTransType.SelectedValue = "0") Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.KEY_FLG = ddlTransType.SelectedValue)
                End If

                If Not (ddlDepartment.SelectedValue = "0") Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = ddlDepartment.SelectedValue)
                End If

                If Not (ddlBranch.SelectedIndex = 0) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.BRANCHCODE.Contains(ddlBranch.SelectedValue))
                End If

                'If Not (txtYear.Text = "") Then
                '    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTYEAR.Value = CInt(txtYear.Text))
                'End If
                If Not (txtNo.Text = "") Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DCN_DOCUMENTNO.Contains(txtNo.Text))
                End If

                If Not (rdoUserConfirm.SelectedValue = -1) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISCONFIRM = rdoUserConfirm.SelectedValue)
                End If

                If rdolTypeDate.SelectedValue = 1 Then
                    If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                        Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text) & " 00:00:00"
                        Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text) & " 23:59:59"
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.P3_TRANS_DATE >= strStartDate And tbl_Amlo.P3_TRANS_DATE <= strEndDate)
                    End If
                Else

                    If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                        Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text) & " 00:00:00"
                        Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text) & " 23:59:59"
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.CREATEDATE >= strStartDate And tbl_Amlo.CREATEDATE <= strEndDate)
                    End If
                End If

                If Not (rdolIsSend.SelectedValue = 0) Then
                    If (rdolIsSend.SelectedValue = 1) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 1)
                    ElseIf (rdolIsSend.SelectedValue = 2) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 0)
                    End If

                End If

                dsAmloXML.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
                If (qryAmlo.Count = 0) Then
                    lblMsg.Visible = True
                    Exit Sub
                Else
                    lblMsg.Visible = False
                End If

                Dim AmloXML As New clsAmloXML
                Dim path As String
                Dim iCount, iAllRec As Integer
                Dim CountChk As Integer = 0
                iAllRec = dsAmloXML.Tables("Amlo").Rows.Count

                For iCount = 0 To iAllRec - 1
                    Dim chkConfirmGen As CheckBox = CType(grdAmlo1059.Rows(iCount).FindControl("CheckBox1"), CheckBox)
                    If (chkConfirmGen.Checked) Then
                        CountChk += 1
                    End If
                Next
                '////////////////////////
                path = "C:\EXPORTXML\AMLO1059.xml"
                FileCopy("C:\EXPORTXML\Template\AMLO1059.xml", "C:\EXPORTXML\AMLO1059.xml")

                iAllRec = dsAmloXML.Tables("Amlo").Rows.Count

                Dim idList = New List(Of Integer)
                For iCount = 0 To iAllRec - 1

                    Dim chkConfirmGen As CheckBox = CType(grdAmlo1059.Rows(iCount).FindControl("CheckBox1"), CheckBox)
                    If (chkConfirmGen.Checked) Then
                        CountChk += 1

                        Dim hdfId = CType(grdAmlo1059.Rows(iCount).FindControl("hdfId"), HiddenField)
                        idList.Add(CInt(hdfId.Value))
                    End If
                Next

                If Not (AmloXML.AddAMLO1059nodeWithAcid(path, idList, CountChk)) Then
                    Exit Sub
                End If

                DownloadFile("C:\ExportXML\AMLO1059.xml", "AMLO09" & Now.Date.ToString("yyyyMMdd") & ".xml", True)
                ClearObj()
            End If

        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                UpdatePanel1.GetType(), _
                                                "clientScript", _
                                                "alert('" + ex.Message.Replace("'", "").Replace("""", "") + "')", _
                                                True)
        End Try
    End Sub

    Private Enum enmTypedata
        typeString = 1
        typeInt = 2
        typeDouble = 3
        typeDate = 4
        typeother = 5
    End Enum

    Private Function setValueDefault(ByVal strValue As String, ByVal DataType As enmTypedata) As String
        Dim strDefault As String = ""
        Dim dateFormat As New clsDateFormat

        If (strValue.Trim = "-" Or strValue.Trim = Nothing Or strValue.Trim = "") Then
            strDefault = "N/A"
        Else
            If DataType = enmTypedata.typeString Then
                strDefault = strValue
            ElseIf DataType = enmTypedata.typeInt Then
                strDefault = strValue
            ElseIf DataType = enmTypedata.typeDouble Then
                strDefault = strValue
            ElseIf DataType = enmTypedata.typeDate Then
                strDefault = dateFormat.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, strValue)
            ElseIf DataType = enmTypedata.typeother Then
                strDefault = strValue
            End If
        End If
        Return strDefault
    End Function



    Private Sub DownloadFile(ByVal ffullPath As String, ByVal fname As String, ByVal forceDownload As Boolean)

        If (forceDownload) Then
            Response.AppendHeader("content-disposition", _
            "attachment; filename=" + fname)
        End If
        Response.ContentType = "text/plain"


        Response.WriteFile(ffullPath)
        Response.End()

    End Sub

    Protected Sub rdolTypeApp_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rdolTypeApp.SelectedIndexChanged
        'NewObjAmlo()
        enableBtn(rdolTypeApp.SelectedValue)
    End Sub

    Protected Sub btnComplete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnComplete.Click
        clsDataTable = New clsLINQToDataTable
        clsFormatDate = New clsDateFormat
        Dim UserRight As clsSessionMenuRight
        UserRight = Session("clsMenuRight")


        NewObjAmlo()

        If (ddlRrptType.SelectedValue = "M51") Then
            AMLO101Entity.Connection.Open()
            Using transaction As System.Data.Common.DbTransaction = AMLO101Entity.Connection.BeginTransaction()
                Dim icount As Integer = grdAmlo.Rows.Count
                Try
                    For i = 0 To grdAmlo.Rows.Count - 1
                        Dim txtSeq As String = CType(grdAmlo.Rows(i).FindControl("hdfRptSeq"), HiddenField).Value
                        Dim chkConfirmIsSend As CheckBox = CType(grdAmlo.Rows(i).FindControl("CheckBox2"), CheckBox)
                        Dim qryAmlo = (From tblAmlo101 In AMLO101Entity.AMLO101 Where tblAmlo101.RPTSEQ = txtSeq).ToList

                        If (chkConfirmIsSend.Checked) Then
                            qryAmlo(0).ISSEND = Convert.ToInt32(1)
                            AMLO101Entity.SaveChanges()
                        End If

                    Next
                    transaction.Commit()
                    ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                       UpdatePanel1.GetType(), _
                                                       "clientScript", _
                                                       "alert('ได้ทำการยืนยันการส่งเสร็จเรียบร้อย')", _
                                                       True)
                Catch ex As Exception
                    transaction.Rollback()
                    ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                      UpdatePanel1.GetType(), _
                                                      "clientScript", _
                                                      "alert('เกิดข้อผิดพลาดในยืนยันการส่ง')", _
                                                      True)
                End Try

            End Using
            AMLO101Entity.Connection.Close()
        ElseIf (ddlRrptType.SelectedValue = "M52") Then
            AMLO102Entity.Connection.Open()
            Using transaction As System.Data.Common.DbTransaction = AMLO102Entity.Connection.BeginTransaction()
                Dim icount As Integer = grdAmlo.Rows.Count
                Try
                    For i = 0 To grdAmlo.Rows.Count - 1
                        Dim txtSeq As String = CType(grdAmlo.Rows(i).FindControl("hdfRptSeq"), HiddenField).Value
                        Dim chkConfirmIsSend As CheckBox = CType(grdAmlo.Rows(i).FindControl("CheckBox2"), CheckBox)
                        Dim qryAmlo = (From tblAmlo102 In AMLO102Entity.AMLO102 Where tblAmlo102.RPTSEQ = txtSeq).ToList

                        If (chkConfirmIsSend.Checked) Then
                            qryAmlo(0).ISSEND = Convert.ToInt32(1)
                            AMLO102Entity.SaveChanges()
                        End If

                    Next
                    transaction.Commit()
                    ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                      UpdatePanel1.GetType(), _
                                                      "clientScript", _
                                                      "alert('ได้ทำการยืนยันการส่งเสร็จเรียบร้อย')", _
                                                      True)
                Catch ex As Exception
                    transaction.Rollback()
                    ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                    UpdatePanel1.GetType(), _
                                                    "clientScript", _
                                                    "alert('เกิดข้อผิดพลาดในยืนยันการส่ง')", _
                                                    True)
                End Try
            End Using
            AMLO102Entity.Connection.Close()
        ElseIf (ddlRrptType.SelectedValue = "M53") Then
            AMLO103Entity.Connection.Open()
            Using transaction As System.Data.Common.DbTransaction = AMLO103Entity.Connection.BeginTransaction()
                Dim icount As Integer = grdAmlo.Rows.Count
                Try
                    For i = 0 To grdAmlo.Rows.Count - 1
                        Dim txtSeq As String = CType(grdAmlo.Rows(i).FindControl("hdfRptSeq"), HiddenField).Value
                        Dim chkConfirmIsSend As CheckBox = CType(grdAmlo.Rows(i).FindControl("CheckBox2"), CheckBox)
                        Dim qryAmlo = (From tblAmlo103 In AMLO103Entity.AMLO103 Where tblAmlo103.RPTSEQ = txtSeq).ToList

                        If (chkConfirmIsSend.Checked) Then
                            qryAmlo(0).ISSEND = Convert.ToInt32(1)
                            AMLO103Entity.SaveChanges()
                        End If

                    Next
                    transaction.Commit()
                    ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                      UpdatePanel1.GetType(), _
                                                      "clientScript", _
                                                      "alert('ได้ทำการยืนยันการส่งเสร็จเรียบร้อย')", _
                                                      True)
                Catch ex As Exception
                    transaction.Rollback()
                    ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                    UpdatePanel1.GetType(), _
                                                    "clientScript", _
                                                    "alert('เกิดข้อผิดพลาดในยืนยันการส่ง')", _
                                                    True)
                End Try
            End Using
            AMLO103Entity.Connection.Close()
        ElseIf (ddlRrptType.SelectedValue = "M57") Then
            AMLO1059Entity.Connection.Open()
            Using transaction As System.Data.Common.DbTransaction = AMLO1059Entity.Connection.BeginTransaction()
                Dim icount As Integer = grdAmlo1059.Rows.Count
                Try
                    For i = 0 To grdAmlo1059.Rows.Count - 1
                        Dim txtSeq As String = CType(grdAmlo1059.Rows(i).FindControl("hdfId"), HiddenField).Value
                        Dim chkConfirmIsSend As CheckBox = CType(grdAmlo1059.Rows(i).FindControl("CheckBox2"), CheckBox)
                        Dim qryAmlo = (From tblAmlo In AMLO1059Entity.AMLO1059 Where tblAmlo.ID = txtSeq).ToList

                        If (chkConfirmIsSend.Checked) Then
                            qryAmlo(0).ISSEND = Convert.ToInt32(1)
                            AMLO1059Entity.SaveChanges()
                        End If

                    Next
                    transaction.Commit()
                    ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                      UpdatePanel1.GetType(), _
                                                      "clientScript", _
                                                      "alert('ได้ทำการยืนยันการส่งเสร็จเรียบร้อย')", _
                                                      True)
                Catch ex As Exception
                    transaction.Rollback()
                    ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                    UpdatePanel1.GetType(), _
                                                    "clientScript", _
                                                    "alert('เกิดข้อผิดพลาดในยืนยันการส่ง')", _
                                                    True)
                End Try
            End Using
            AMLO1059Entity.Connection.Close()
        End If


        'grdAmlo.DataSource = dsAmlo.Tables("Amlo")
        'grdAmlo.DataBind()
        'grdAmlo.Columns(9).Visible = False
        'grdAmlo.Columns(10).Visible = False
        IsDataBindCompleteExportXML()


    End Sub

    Protected Sub ddlDepartment_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDepartment.SelectedIndexChanged
        Dim clsDropDown As New clsMasterDropDown
        clsSessionMenuRight = Session("ClsMenuRight")


        'If Not (ddlDepartment.SelectedValue = "0") Then
        '    clsDropDown.setBranchDdlSearch(ddlBranch, strDeptCode:=ddlDepartment.SelectedValue, strBranchCode:=clsSessionMenuRight.getBranchCode, strRoleCode:=clsSessionMenuRight.getRoleGroup, strZoneCode:=clsSessionMenuRight.getZoneCode)
        '    'clsMasterDropDown.setUserRightToDdlSearch(ddlUserRight, clsSessionMenuRight.getRoleGroup)

        '    clsDropDown.setUserRightToDdlSearch(ddlUserRight, strRoleCode:=clsSessionMenuRight.getRoleGroup, strDepCode:=ddlDepartment.SelectedValue)
        'Else
        '    clsDropDown.setBranchDdlSearch(ddlBranch, strDeptCode:=ddlDepartment.SelectedValue, strBranchCode:=clsSessionMenuRight.getBranchCode, strRoleCode:=clsSessionMenuRight.getRoleGroup, strZoneCode:=clsSessionMenuRight.getZoneCode)
        '    clsDropDown.setUserRightToDdlSearch(ddlUserRight, strRoleCode:=clsSessionMenuRight.getRoleGroup)
        '    ddlBranch.SelectedValue = "0"
        '    ddlUserRight.SelectedValue = "0"
        'End If


        If Not (ddlDepartment.SelectedValue = "0") Then
            clsDropDown.setBranchDdlSearch(ddlBranch, strDeptCode:=ddlDepartment.SelectedValue, strBranchCode:=clsSessionMenuRight.getBranchCode, strRoleCode:=clsSessionMenuRight.getRoleGroup, strZoneCode:=clsSessionMenuRight.getZoneCode)
        Else
            clsDropDown.setBranchDdlSearch(ddlBranch, strDeptCode:=ddlDepartment.SelectedValue, strBranchCode:=clsSessionMenuRight.getBranchCode, strRoleCode:=clsSessionMenuRight.getRoleGroup, strZoneCode:=clsSessionMenuRight.getZoneCode)
        End If

        clsDropDown = Nothing

    End Sub

    Protected Sub ddlRrptType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlRrptType.SelectedIndexChanged
        If (ddlRrptType.SelectedValue = "M51") Then
            lblTransType.Visible = False
            ddlTransType.Visible = False
            grdAmlo.Columns(5).Visible = True
            lblsignType.Visible = True
            rdolSignType.Visible = True
            lblUserConfirm.Visible = False
            rdoUserConfirm.Visible = False
            btnPrint.Visible = True
            btnPrintFull.Visible = False
            btnPrint1.Visible = False
            btnPrint2.Visible = False
            grdAmlo.DataSource = Nothing
            grdAmlo.DataBind()

            grdAmlo.Visible = True
            grdAmlo1059.Visible = False
            ' lblTotal.Visible = False
        ElseIf (ddlRrptType.SelectedValue = "M52") Then
            lblTransType.Visible = False
            ddlTransType.Visible = False
            grdAmlo.Columns(5).Visible = True
            lblsignType.Visible = True
            rdolSignType.Visible = True
            lblUserConfirm.Visible = False
            rdoUserConfirm.Visible = False
            btnPrint.Visible = True
            btnPrintFull.Visible = False
            btnPrint1.Visible = False
            btnPrint2.Visible = False
            grdAmlo.DataSource = Nothing
            grdAmlo.DataBind()

            grdAmlo.Visible = True
            grdAmlo1059.Visible = False
            '  lblTotal.Visible = False
        ElseIf (ddlRrptType.SelectedValue = "M53") Then
            lblTransType.Visible = False
            ddlTransType.Visible = False
            grdAmlo.Columns(5).Visible = False
            lblsignType.Visible = False
            rdolSignType.Visible = False
            lblUserConfirm.Visible = False
            rdoUserConfirm.Visible = False
            btnPrint.Visible = True
            btnPrintFull.Visible = False
            btnPrint1.Visible = False
            btnPrint2.Visible = False
            grdAmlo.DataSource = Nothing
            grdAmlo.DataBind()

            grdAmlo.Visible = True
            grdAmlo1059.Visible = False
            '  lblTotal.Visible = False
        ElseIf (ddlRrptType.SelectedValue = "M57") Then
            lblTransType.Visible = True
            ddlTransType.Visible = True
            grdAmlo.Columns(5).Visible = False
            lblsignType.Visible = False
            rdolSignType.Visible = False
            lblUserConfirm.Visible = True
            rdoUserConfirm.Visible = True
            btnPrint.Visible = False
            btnPrintFull.Visible = True
            btnPrint1.Visible = True
            btnPrint2.Visible = True
            grdAmlo.DataSource = Nothing
            grdAmlo.DataBind()
            grdAmlo.Visible = False

            grdAmlo1059.DataSource = Nothing
            grdAmlo1059.DataBind()
            grdAmlo1059.Visible = True
        End If

        If Not rdolTypeApp.SelectedValue = "" Then
            enableBtn(rdolTypeApp.SelectedValue)
        End If

    End Sub


    'Protected Sub grdAmlo_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdAmlo.PageIndexChanging
    '    grdAmlo.PageIndex = e.NewPageIndex
    '    clearGvIndex()
    '    DataBind()
    'End Sub

    Protected Sub grdAmlo_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grdAmlo.RowDataBound
        clsFormatDate = New clsDateFormat
        If (e.Row.RowType.ToString = "DataRow") Then
            If Not (e.Row.Cells(4).Text = Nothing) Then
                e.Row.Cells(4).Text = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.Th, e.Row.Cells(4).Text)
            End If

        End If
    End Sub

    Protected Sub grdAmlo_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs) Handles grdAmlo.SelectedIndexChanging
        intBeforeItemIndex = grdAmlo.SelectedIndex
    End Sub

    Protected Sub grdAmlo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdAmlo.SelectedIndexChanged
        If (grdAmlo.SelectedIndex = intBeforeItemIndex) Then
            clearGvIndex()
        End If
    End Sub

    Protected Sub grdAmlo_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles grdAmlo.Sorting
        Dim dt = TryCast(dsAmlo.Tables("Amlo"), DataTable)

        If dt IsNot Nothing Then

            'Sort the data.
            dt.DefaultView.Sort = e.SortExpression & " " & GetSortDirection(e.SortExpression)
            grdAmlo.DataSource = dsAmlo.Tables("Amlo")
            grdAmlo.DataBind()
            clearGvIndex()
        End If
    End Sub


    Protected Sub chkAll_GenXml_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim chk As CheckBox = CType(grdAmlo.HeaderRow.FindControl("chkAll_GenXml"), CheckBox)
        If (chk.Checked) Then
            For i As Int16 = 0 To grdAmlo.Rows.Count - 1
                Dim chkrow As CheckBox = CType(grdAmlo.Rows(i).FindControl("CheckBox1"), CheckBox)
                chkrow.Checked = True

            Next
        Else

            For i As Int16 = 0 To grdAmlo.Rows.Count - 1
                Dim chkrow As CheckBox = CType(grdAmlo.Rows(i).FindControl("CheckBox1"), CheckBox)
                chkrow.Checked = False

            Next

        End If
    End Sub

    Protected Sub chkAll_Confirm_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim chk As CheckBox = CType(grdAmlo.HeaderRow.FindControl("chkAll_Confirm"), CheckBox)
        If (chk.Checked) Then
            For i As Int16 = 0 To grdAmlo.Rows.Count - 1
                Dim chkrow As CheckBox = CType(grdAmlo.Rows(i).FindControl("CheckBox2"), CheckBox)
                chkrow.Checked = True

            Next
        Else

            For i As Int16 = 0 To grdAmlo.Rows.Count - 1
                Dim chkrow As CheckBox = CType(grdAmlo.Rows(i).FindControl("CheckBox2"), CheckBox)
                chkrow.Checked = False

            Next

        End If
    End Sub

    Protected Sub chkAll_GenXml1059_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim chk As CheckBox = CType(grdAmlo1059.HeaderRow.FindControl("chkAll_GenXml"), CheckBox)
        If (chk.Checked) Then
            For i As Int16 = 0 To grdAmlo1059.Rows.Count - 1
                Dim chkrow As CheckBox = CType(grdAmlo1059.Rows(i).FindControl("CheckBox1"), CheckBox)
                chkrow.Checked = True

            Next
        Else

            For i As Int16 = 0 To grdAmlo1059.Rows.Count - 1
                Dim chkrow As CheckBox = CType(grdAmlo1059.Rows(i).FindControl("CheckBox1"), CheckBox)
                chkrow.Checked = False

            Next

        End If
    End Sub

    Protected Sub chkAll_Confirm1059_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim chk As CheckBox = CType(grdAmlo1059.HeaderRow.FindControl("chkAll_Confirm"), CheckBox)
        If (chk.Checked) Then
            For i As Int16 = 0 To grdAmlo1059.Rows.Count - 1
                Dim chkrow As CheckBox = CType(grdAmlo1059.Rows(i).FindControl("CheckBox2"), CheckBox)
                chkrow.Checked = True

            Next
        Else

            For i As Int16 = 0 To grdAmlo1059.Rows.Count - 1
                Dim chkrow As CheckBox = CType(grdAmlo1059.Rows(i).FindControl("CheckBox2"), CheckBox)
                chkrow.Checked = False

            Next

        End If
    End Sub

    Protected Sub btnGenXMLShort_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGenXMLShort.Click
        Dim dsAmloXML As DataSet
        Dim dsAmloXMLDetail As DataSet
        Dim dsAmloXMLCount As DataSet
        clsFormatDate = New clsDateFormat

        Try
            ' ========================================
            If (grdAmlo.Rows.Count = 0) Then
                ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                         UpdatePanel1.GetType(), _
                                                         "clientScript", _
                                                         "alert('กรุณาเลือกข้อมูลที่ต้องการออกไฟล์ Xml ')", _
                                                         True)
                Exit Sub
            End If
            clsDataTable = New clsLINQToDataTable
            clsFormatDate = New clsDateFormat
            Dim UserRight As clsSessionMenuRight
            UserRight = Session("clsMenuRight")

            dsAmloXML = New DataSet()

            NewObjAmlo()

            If (ddlRrptType.SelectedValue = "M51") Then
                Dim qryAmlo = From tblAmlo In AMLO101Entity.AMLO101 Join tblAmloDetail In AMLO101Entity.AMLODETAIL101 On tblAmlo.RPTSEQ Equals tblAmloDetail.RPTSEQ
                             Where tblAmloDetail.RPTIDX = 1 And tblAmlo.ISCOMPLETE = 1 _
                             Order By tblAmlo.RPTSEQ Descending, tblAmlo.DEPTCODE Descending, tblAmlo.BRANCHCODE Descending _
                             Select tblAmlo.RPTSEQ, tblAmlo.GHBCODE, tblAmlo.RPTYEAR, tblAmlo.RPTNO, tblAmlo.RPTREV, tblAmlo.BRANCHCODE, tblAmlo.SOL_ID, _
tblAmlo.DEPTCODE, tblAmlo.RPTDATE, tblAmlo.TRANSDATE, tblAmlo.TRANSTYPE, tblAmlo.TRANSTYPEOTHER, _
tblAmlo.ACCOUNTNO, tblAmlo.TRANSAMT, tblAmlo.CURRENCY, tblAmlo.GAINPREFIX, tblAmlo.GAINFIRSTNAME, tblAmlo.GAINMIDNAME, _
tblAmlo.GAINLASTNAME, tblAmlo.OBJBIZ, tblAmlo.OBJBIZOTHER, tblAmlo.SAVETYPE, tblAmlo.SIGNTYPE, tblAmlo.SAVEDATE, _
tblAmlo.UPDATEBY, tblAmlo.ISCOMPLETE, tblAmlo.ISDELETE, tblAmlo.ISCONFIRM, tblAmlo.ISSEND, _
tblAmloDetail.RPTIDX, tblAmloDetail.RPTTYPE, tblAmloDetail.RPTIDXTYPE, _
tblAmloDetail.UNIQUETYPE, tblAmloDetail.UNIQUEID, tblAmloDetail.PREFIX, tblAmloDetail.FIRSTNAME, _
                tblAmloDetail.MIDNAME, tblAmloDetail.LASTNAME, tblAmloDetail.FORMALADDR, tblAmloDetail.FORMALPHONE, _
                tblAmloDetail.FORMALFAX, tblAmloDetail.OCCUPATIONTYPE, tblAmloDetail.OCCUPATIONTYPEOTHER, _
                tblAmloDetail.WORKADDR, tblAmloDetail.WORKPHONE, tblAmloDetail.WORKFAX, tblAmloDetail.BIZTYPE, _
                tblAmloDetail.BIZTYPEOTHER, tblAmloDetail.CONTACTADDR, tblAmloDetail.CONTACTPHONE, _
                tblAmloDetail.CONTACTFAX, tblAmloDetail.CUSTIDTYPE, tblAmloDetail.CUSTIDTYPEOTHER, _
                tblAmloDetail.CUSTID, tblAmloDetail.EVBY, tblAmloDetail.EVGEN, tblAmloDetail.EVGENTEXT, _
                tblAmloDetail.EVEXPIRE, tblAmloDetail.EVEXPIRETEXT

                'Select tblAmlo.CURRENCY, tblAmlo.TRANSAMT, tblAmlo.ACCOUNTNO, tblAmlo.TRANSTYPEOTHER, tblAmloDetail.EVBY, _
                'tblAmloDetail.EVGEN, tblAmloDetail.EVEXPIRE, tblAmloDetail.CUSTIDTYPE, tblAmloDetail.CUSTIDTYPEOTHER, _
                'tblAmloDetail.CUSTID, tblAmloDetail.OCCUPATIONTYPE, tblAmloDetail.OCCUPATIONTYPEOTHER, tblAmloDetail.CONTACTADDR, _
                'tblAmloDetail.CONTACTPHONE, tblAmloDetail.CONTACTFAX, tblAmloDetail.FORMALADDR, tblAmloDetail.FORMALPHONE, _
                'tblAmloDetail.FORMALFAX, tblAmloDetail.WORKADDR, tblAmloDetail.WORKPHONE, tblAmloDetail.WORKFAX, tblAmlo.RPTNO, _
                'tblAmlo.RPTREV, tblAmlo.RPTDATE, tblAmlo.RPTYEAR, tblAmlo.ISSEND, tblAmloDetail.PREFIX, tblAmloDetail.FIRSTNAME, _
                'tblAmloDetail.MIDNAME, tblAmloDetail.LASTNAME, tblAmloDetail.UNIQUETYPE, tblAmloDetail.UNIQUEID, tblAmlo.BRANCHCODE, _
                'tblAmlo.TRANSDATE, tblAmlo.TRANSTYPE, tblAmlo.SIGNTYPE, tblAmlo.UPDATEBY, tblAmlo.SAVEDATE, tblAmlo.RPTSEQ, _
                'tblAmlo.ISCOMPLETE, tblAmlo.ISDELETE, tblAmlo.ISCONFIRM, tblAmlo.GHBCODE

                If Not (ddlDepartment.SelectedValue = "0") Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = ddlDepartment.SelectedValue)
                End If

                If Not (ddlBranch.SelectedIndex = 0) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.BRANCHCODE.Contains(ddlBranch.SelectedValue))
                End If

                'If Not (txtYear.Text = "") Then
                '    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTYEAR.Value = CInt(txtYear.Text))
                'End If

                If Not (txtNo.Text = "") Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTNO.Contains(txtNo.Text))
                End If

                If rdolTypeDate.SelectedValue = 1 Then
                    If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                        Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                        Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.TRANSDATE >= strStartDate And tbl_Amlo.TRANSDATE <= strEndDate)
                    End If
                Else
                    If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                        Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                        Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SAVEDATE >= strStartDate And tbl_Amlo.SAVEDATE <= strEndDate)
                    End If
                End If

                If Not (rdolSignType.SelectedValue = 0) Then
                    If (rdolSignType.SelectedValue = 1) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 1)
                    ElseIf (rdolSignType.SelectedValue = 2) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 0)
                    End If

                End If

                If Not (rdolIsSend.SelectedValue = 0) Then
                    If (rdolIsSend.SelectedValue = 1) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 1)
                    ElseIf (rdolIsSend.SelectedValue = 2) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 0)
                    End If

                End If

                dsAmloXML.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
                If (qryAmlo.Count = 0) Then
                    lblMsg.Visible = True
                Else
                    lblMsg.Visible = False
                End If

                Dim AmloXML As New clsAmloXMLShort
                Dim path As String
                Dim iCount, iAllRec As Integer
                Dim CountChk As Integer = 0
                Dim CountChkToGen As Integer = 0
                Dim nAmlo1Data As clsAmlo1Data = New clsAmlo1Data
                Dim nAmlo1Psn(9) As clsAmlo1Psn
                iAllRec = dsAmloXML.Tables("Amlo").Rows.Count

                For iCount = 0 To iAllRec - 1
                    Dim chkConfirmGen As CheckBox = CType(grdAmlo.Rows(iCount).FindControl("CheckBox1"), CheckBox)
                    If (chkConfirmGen.Checked) Then
                        CountChk += 1
                    End If
                Next
                path = "C:\EXPORTXML\AMLO101.xml"
                FileCopy("C:\EXPORTXML\Template\AMLO101.xml", "C:\EXPORTXML\AMLO101.xml")
                For iCount = 0 To iAllRec - 1

                    Dim chkConfirmGen As CheckBox = CType(grdAmlo.Rows(iCount).FindControl("CheckBox1"), CheckBox)
                    If (chkConfirmGen.Checked) Then
                        CountChkToGen += 1
                        'ชนิดของรายงาน 1-01
                        nAmlo1Data.p01_type = "1-01"
                        'เลขที่รายงาน 
                        Dim strRptNo As String = ""

                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("GHBCODE").ToString
                        'strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("DEPTCODE").ToString
                        'strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("BRANCHCODE").ToString.Substring(0, 3)
                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("SOL_ID").ToString
                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTYEAR").ToString
                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTNO").ToString


                        nAmlo1Data.p02_reportid = strRptNo
                        'ครั้งที่แก้ไข/เพิ่มเติม  -ถ้าไม่ระบุจะถือเป็นฉบับแรก
                        nAmlo1Data.p03_revision = dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTREV").ToString
                        'วันที่รายงาน -วันที่/เดือน/ปี พ.ศ.(4หลัก)
                        nAmlo1Data.p04_date = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTDATE").ToString, enmTypedata.typeDate)
                        '=================================
                        'ส่วนที่ 1. ผู้ทำธุรกรรม 
                        nAmlo1Data.p101_cnd_name_prefix = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("PREFIX").ToString, enmTypedata.typeString)
                        nAmlo1Data.p102_cnd_name_first = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FIRSTNAME").ToString, enmTypedata.typeString)
                        nAmlo1Data.p103_cnd_name_middle = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("MIDNAME").ToString, enmTypedata.typeString)
                        nAmlo1Data.p104_cnd_name_last = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("LASTNAME").ToString, enmTypedata.typeString)

                        nAmlo1Data.p105_cnd_id_no = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("UNIQUEID").ToString, enmTypedata.typeString)
                        nAmlo1Data.p106_cnd_id_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("UNIQUETYPE").ToString, enmTypedata.typeString)

                        nAmlo1Data.p107_cnd_location_addr = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FORMALADDR").ToString, enmTypedata.typeString)
                        nAmlo1Data.p108_cnd_location_phone = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FORMALPHONE").ToString, enmTypedata.typeString)
                        nAmlo1Data.p109_cnd_location_fax = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FORMALFAX").ToString, enmTypedata.typeString)


                        'ระบุอาชีพ 1-เจ้าของกิจการ, 2-ข้าราชการ, 3-รับจ้าง, 4-พนักงานรัฐวิสาหกิจ, 5-อาชีพอิสระ, 6-นิสิต นักศึกษา, 99-อื่นๆ
                        nAmlo1Data.p110_cnd_office_occ = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OCCUPATIONTYPE").ToString, enmTypedata.typeString)
                        nAmlo1Data.p111_cnd_office_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OCCUPATIONTYPEOTHER").ToString, enmTypedata.typeString)
                        nAmlo1Data.p112_cnd_office_org = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("WORKADDR").ToString, enmTypedata.typeString)
                        nAmlo1Data.p113_cnd_office_phone = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("WORKPHONE").ToString, enmTypedata.typeString)
                        nAmlo1Data.p114_cnd_office_fax = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("WORKFAX").ToString, enmTypedata.typeString)


                        nAmlo1Data.p115_cnd_contact_addr = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CONTACTADDR").ToString, enmTypedata.typeString)
                        nAmlo1Data.p116_cnd_contact_phone = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CONTACTPHONE").ToString, enmTypedata.typeString)
                        nAmlo1Data.p117_cnd_contact_fax = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CONTACTFAX").ToString, enmTypedata.typeString)


                        'ชนิดหลักฐาน 1-บัตรประจำตัวประชาชน (ต้องเป็นตัวเลขและมีขนาด 13 หลักเท่านั้น), 2-บัตรประจำตัวผู้เสียภาษีอากร (ต้องเป็นตัวเลขและมีขนาด 10 หลักเท่านั้น), 3-หนังสือเดินทาง, 4-ใบสำคัญคนต่างด้าว,
                        ' 5- หนังสือรับรองข้อความในทะเบียน, 6 - บัตรที่ออกโดยองค์กรรัฐ, 7 - หนังสือสุทธิสำหรับพระภิกษุ, 8 -บัตรประจำตัวชนกลุ่มน้อย, 99 - อื่นๆ 
                        'nAmlo1Data.p118_cnd_iddoc_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CUSTIDTYPE").ToString, enmTypedata.typeString)
                        'nAmlo1Data.p119_cnd_iddoc_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CUSTIDTYPEOTHER").ToString, enmTypedata.typeString)
                        'nAmlo1Data.p120_cnd_iddoc_no = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CUSTID").ToString, enmTypedata.typeString)

                        'nAmlo1Data.p121_cnd_iddoc_by = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVBY").ToString, enmTypedata.typeString)

                        'Try

                        '    ' nAmlo1Data.p122_cnd_iddoc_date = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGEN").ToString, enmTypedata.typeDate)
                        '    nAmlo1Data.p122_cnd_iddoc_date = setValueDefault(If(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGENTEXT").ToString = "-", dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGENTEXT").ToString, clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGENTEXT").ToString)), enmTypedata.typeother)
                        'Catch ex As Exception

                        'End Try

                        'Try
                        '    'nAmlo1Data.p123_cnd_iddoc_expire = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRE").ToString, enmTypedata.typeDate)
                        '    nAmlo1Data.p123_cnd_iddoc_expire = setValueDefault(If(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRETEXT").ToString = "-", dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRETEXT").ToString, clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRETEXT").ToString)), enmTypedata.typeother)
                        'Catch ex As Exception

                        'End Try


                        'ส่วนที่ 3. ข้อเท็จจริงเกี่ยวกับธุรกรรม

                        nAmlo1Data.p301_tsc_date = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSDATE").ToString, enmTypedata.typeDate)
                        nAmlo1Data.p302_tsc_item_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSTYPE").ToString, enmTypedata.typeString)
                        nAmlo1Data.p303_tsc_item_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSTYPEOTHER").ToString, enmTypedata.typeString)
                        nAmlo1Data.p304_tsc_item_acccno = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCOUNTNO").ToString, enmTypedata.typeString)
                        nAmlo1Data.p305_tsc_item_cur = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CURRENCY").ToString, enmTypedata.typeString)
                        nAmlo1Data.p306_tsc_item_amt = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSAMT").ToString, enmTypedata.typeString)
                        nAmlo1Data.p307_tsc_name_prefix = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINPREFIX").ToString, enmTypedata.typeString)
                        nAmlo1Data.p308_tsc_name_first = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINFIRSTNAME").ToString, enmTypedata.typeString)
                        nAmlo1Data.p309_tsc_name_middle = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINMIDNAME").ToString, enmTypedata.typeString)
                        nAmlo1Data.p310_tsc_name_last = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINLASTNAME").ToString, enmTypedata.typeString)
                        nAmlo1Data.p311_tsc_obj_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OBJBIZ").ToString, enmTypedata.typeString)
                        nAmlo1Data.p312_tsc_obj_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OBJBIZOTHER").ToString, enmTypedata.typeString)
                        nAmlo1Data.p401_record_writer = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("SAVETYPE").ToString, enmTypedata.typeString)
                        nAmlo1Data.p402_record_sign = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("SIGNTYPE").ToString, enmTypedata.typeString)
                        nAmlo1Data.p403_record_date = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("SAVEDATE").ToString, enmTypedata.typeDate)


                        Dim mRptSeq As Integer = Convert.ToInt32(dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTSEQ").ToString)
                        Dim qryAmloDetailCount = From tblAmloDetail In AMLO101Entity.AMLODETAIL101
                         Where tblAmloDetail.RPTSEQ = mRptSeq And tblAmloDetail.RPTIDX > 1
                         Group By tblAmloDetail.RPTSEQ Into MaxDetail = Max(tblAmloDetail.RPTIDX)

                        dsAmloXMLCount = New DataSet()
                        '    NewObjAmloDetail()
                        dsAmloXMLCount.Tables.Add(clsDataTable.LINQToDataTable(qryAmloDetailCount, "AmloCount").Copy)


                        Dim iPsnCount As Integer = 0
                        If dsAmloXMLCount.Tables("AmloCount").Rows.Count > 0 Then
                            Dim mMaxRptDetail As Integer = Convert.ToInt32(dsAmloXMLCount.Tables("AmloCount").Rows(0).Item("MaxDetail").ToString)
                            Dim qryAmloDetail = From tblAmloDetail In AMLO101Entity.AMLODETAIL101
                            Where tblAmloDetail.RPTSEQ = mRptSeq And tblAmloDetail.RPTIDX > 1
                            Order By tblAmloDetail.RPTIDX
                             Select tblAmloDetail.EVBY, tblAmloDetail.EVGEN, tblAmloDetail.EVEXPIRE, tblAmloDetail.CUSTIDTYPE, tblAmloDetail.CUSTIDTYPEOTHER, _
                                tblAmloDetail.CUSTID, tblAmloDetail.OCCUPATIONTYPE, tblAmloDetail.OCCUPATIONTYPEOTHER, tblAmloDetail.CONTACTADDR, _
                                 tblAmloDetail.CONTACTPHONE, tblAmloDetail.CONTACTFAX, tblAmloDetail.FORMALADDR, tblAmloDetail.FORMALPHONE, tblAmloDetail.BIZTYPE, _
                            tblAmloDetail.FORMALFAX, tblAmloDetail.WORKADDR, tblAmloDetail.WORKPHONE, tblAmloDetail.WORKFAX, tblAmloDetail.RPTIDXTYPE, _
                            tblAmloDetail.PREFIX, tblAmloDetail.FIRSTNAME, tblAmloDetail.MIDNAME, tblAmloDetail.LASTNAME, tblAmloDetail.UNIQUETYPE, tblAmloDetail.UNIQUEID

                            dsAmloXMLDetail = New DataSet()
                            dsAmloXMLDetail.Tables.Add(clsDataTable.LINQToDataTable(qryAmloDetail, "AmloDetail").Copy)

                            For Each dr As DataRow In dsAmloXMLDetail.Tables("AmloDetail").Rows
                                iPsnCount = iPsnCount + 1
                                nAmlo1Psn(iPsnCount) = New clsAmlo1Psn(iPsnCount)

                                nAmlo1Psn(iPsnCount).p201_psn_relation = setValueDefault(dr("RPTIDXTYPE").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p202_psn_name_prefix = setValueDefault(dr("PREFIX").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p203_psn_name_first = setValueDefault(dr("FIRSTNAME").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p204_psn_name_middle = setValueDefault(dr("MIDNAME").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p205_psn_name_last = setValueDefault(dr("LASTNAME").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p206_psn_id_no = setValueDefault(dr("UNIQUEID").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p207_psn_id_type = setValueDefault(dr("UNIQUETYPE").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p208_psn_location_addr = setValueDefault(dr("FORMALADDR").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p209_psn_location_phone = setValueDefault(dr("FORMALPHONE").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p210_psn_location_fax = setValueDefault(dr("FORMALFAX").ToString, enmTypedata.typeString)


                                'ระบุอาชีพ 1-เจ้าของกิจการ, 2-ข้าราชการ, 3-รับจ้าง, 4-พนักงานรัฐวิสาหกิจ, 5-อาชีพอิสระ, 6-นิสิต นักศึกษา, 99-อื่นๆ
                                nAmlo1Psn(iPsnCount).p211_psn_office_occ = setValueDefault(dr("OCCUPATIONTYPE").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p212_psn_office_org = setValueDefault(dr("WORKADDR").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p213_psn_office_phone = setValueDefault(dr("WORKPHONE").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p214_psn_office_fax = setValueDefault(dr("WORKFAX").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p215_psn_office_type = setValueDefault(dr("BIZTYPE").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p216_psn_office_desc = setValueDefault(dr("OCCUPATIONTYPEOTHER").ToString, enmTypedata.typeString)

                                nAmlo1Psn(iPsnCount).p217_psn_contact_addr = setValueDefault(dr("CONTACTADDR").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p218_psn_contact_phone = setValueDefault(dr("CONTACTPHONE").ToString, enmTypedata.typeString)
                                nAmlo1Psn(iPsnCount).p219_psn_contact_fax = setValueDefault(dr("CONTACTFAX").ToString, enmTypedata.typeString)

                                'ชนิดหลักฐาน 1-บัตรประจำตัวประชาชน (ต้องเป็นตัวเลขและมีขนาด 13 หลักเท่านั้น), 2-บัตรประจำตัวผู้เสียภาษีอากร (ต้องเป็นตัวเลขและมีขนาด 10 หลักเท่านั้น), 3-หนังสือเดินทาง, 4-ใบสำคัญคนต่างด้าว,
                                ' 5- หนังสือรับรองข้อความในทะเบียน, 6 - บัตรที่ออกโดยองค์กรรัฐ, 7 - หนังสือสุทธิสำหรับพระภิกษุ, 8 -บัตรประจำตัวชนกลุ่มน้อย, 99 - อื่นๆ 

                                'nAmlo1Psn(iPsnCount).p220_psn_iddoc_type = setValueDefault(dr("CUSTIDTYPE").ToString, enmTypedata.typeString)
                                'nAmlo1Psn(iPsnCount).p221_psn_iddoc_desc = setValueDefault(dr("CUSTIDTYPEOTHER").ToString, enmTypedata.typeString)
                                'nAmlo1Psn(iPsnCount).p222_psn_iddoc_no = setValueDefault(dr("CUSTID").ToString, enmTypedata.typeString)
                                'nAmlo1Psn(iPsnCount).p223_psn_iddoc_by = setValueDefault(dr("EVBY").ToString, enmTypedata.typeString)
                                'Try
                                '    nAmlo1Psn(iPsnCount).p224_psn_iddoc_date = setValueDefault(If(dr("EVGENTEXT").ToString = "-", dr("EVGENTEXT").ToString, clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dr("EVGENTEXT").ToString)), enmTypedata.typeother)

                                '    'nAmlo1Psn(iPsnCount).p224_psn_iddoc_date = setValueDefault(dr("EVGEN").ToString, enmTypedata.typeDate)
                                '    'nAmlo1Data.p122_cnd_iddoc_date = setValueDefault(clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGENTEXT").ToString), enmTypedata.typeDate)

                                'Catch ex As Exception

                                'End Try
                                'Try
                                '    nAmlo1Psn(iPsnCount).p225_psn_iddoc_expire = setValueDefault(If(dr("EVEXPIRETEXT").ToString = "-", dr("EVEXPIRETEXT").ToString, clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dr("EVEXPIRETEXT").ToString)), enmTypedata.typeother)
                                'Catch ex As Exception

                                'End Try

                            Next

                        End If
                        'iAllRec - 1
                        If Not (AmloXML.AddAMLO1node(path, nAmlo1Data, iPsnCount, nAmlo1Psn, CountChkToGen, CountChk)) Then
                            Exit Sub
                        End If

                    End If

                Next
                DownloadFile("C:\ExportXML\AMLO101.xml", "AMLO101.xml", True)
                ClearObj()

                'MapPath("AMLO101.xml")



            ElseIf (ddlRrptType.SelectedValue = "M52") Then
                'Dim qryAmlo = From tblAmlo In AMLO102Entity.AMLO102 Join tblAmloDetail In AMLO102Entity.AMLODETAIL102 On tblAmlo.RPTSEQ Equals tblAmloDetail.RPTSEQ
                '             Where tblAmloDetail.RPTIDX = 1 And tblAmlo.ISCOMPLETE = 1 _
                '             Order By tblAmlo.RPTSEQ Descending _
                '             Select tblAmlo.RPTYEAR, tblAmlo.RPTREV, tblAmlo.ISSEND, tblAmlo.RPTNO, tblAmloDetail.FIRSTNAME, tblAmloDetail.LASTNAME, tblAmlo.BRANCHCODE, tblAmlo.TRANSDATE, tblAmlo.TRANSTYPE, tblAmlo.SIGNTYPE, tblAmlo.UPDATEBY, tblAmlo.SAVEDATE, tblAmlo.RPTSEQ, tblAmlo.ISCOMPLETE, tblAmlo.ISDELETE, tblAmlo.ISCONFIRM

                Dim qryAmlo = From tblAmlo In AMLO102Entity.AMLO102 Join tblAmloDetail In AMLO102Entity.AMLODETAIL102 On tblAmlo.RPTSEQ Equals tblAmloDetail.RPTSEQ
                          Where tblAmloDetail.RPTIDX = 1 And tblAmlo.ISCOMPLETE = 1 _
                          Order By tblAmlo.RPTSEQ Descending, tblAmlo.DEPTCODE Descending, tblAmlo.BRANCHCODE Descending _
                          Select tblAmlo.RPTSEQ, tblAmlo.GHBCODE, tblAmlo.RPTTYPE, tblAmlo.RPTYEAR, tblAmlo.RPTNO, _
                          tblAmlo.RPTREV, tblAmlo.BRANCHCODE, tblAmlo.SOL_ID, tblAmlo.DEPTCODE, tblAmlo.RPTDATE, tblAmlo.TRANSDATE, _
                          tblAmlo.TRANSTYPE, tblAmlo.TRANSTYPEOTHER, tblAmlo.ASSETTYPE, tblAmlo.ASSETTYPEOTHER, _
                          tblAmlo.ASSETTYPEDETAIL, tblAmlo.TRANSAMT, tblAmlo.TRANSAMTCHAR, tblAmlo.CURRENCY, tblAmlo.ACCOUNTNO, _
                          tblAmlo.ACCOUNTNAME, tblAmlo.ACCPREFIX, tblAmlo.ACCFIRSTNAME, tblAmlo.ACCMIDNAME, tblAmlo.ACCLASTNAME, _
                          tblAmlo.ACCOUNTREF, tblAmlo.ACCOUNTREFNAME, tblAmlo.REFPREFIX, tblAmlo.REFFIRSTNAME, tblAmlo.REFMIDNAME, _
                          tblAmlo.REFLASTNAME, tblAmlo.REFERENCETYPE, tblAmlo.REFERENCETYPEOTHER, tblAmlo.GAINPREFIX, _
                          tblAmlo.GAINFIRSTNAME, tblAmlo.GAINMIDNAME, tblAmlo.GAINLASTNAME, tblAmlo.OBJBIZ, tblAmlo.OBJBIZOTHER, _
                          tblAmlo.SAVETYPE, tblAmlo.SAVEDATE, tblAmlo.FINANORGCODE, tblAmlo.SIGNTYPE, tblAmlo.ISSEND, _
                          tblAmloDetail.RPTIDX, tblAmloDetail.RPTIDXTYPE, tblAmloDetail.UNIQUETYPE, tblAmloDetail.UNIQUEID, _
                          tblAmloDetail.PREFIX, tblAmloDetail.FIRSTNAME, tblAmloDetail.MIDNAME, tblAmloDetail.LASTNAME, _
                          tblAmloDetail.FORMALADDR, tblAmloDetail.FORMALPHONE, tblAmloDetail.FORMALFAX, tblAmloDetail.OCCUPATIONTYPE, _
                          tblAmloDetail.OCCUPATIONTYPEOTHER, tblAmloDetail.WORKADDR, tblAmloDetail.WORKPHONE, tblAmloDetail.WORKFAX, _
                          tblAmloDetail.BIZTYPE, tblAmloDetail.BIZTYPEOTHER, tblAmloDetail.CONTACTADDR, tblAmloDetail.CONTACTPHONE, _
                          tblAmloDetail.CONTACTFAX, tblAmloDetail.CUSTIDTYPE, tblAmloDetail.CUSTIDTYPEOTHER, tblAmloDetail.CUSTID, _
                          tblAmloDetail.EVBY, tblAmloDetail.EVGEN, tblAmloDetail.EVGENTEXT, tblAmloDetail.EVEXPIRE, tblAmloDetail.EVEXPIRETEXT

                If Not (ddlDepartment.SelectedValue = "0") Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = ddlDepartment.SelectedValue)
                End If

                If Not (ddlBranch.SelectedIndex = 0) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.BRANCHCODE.Contains(ddlBranch.SelectedValue))
                End If

                'If Not (txtYear.Text = "") Then
                '    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTYEAR.Value = CInt(txtYear.Text))
                'End If
                If Not (txtNo.Text = "") Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTNO.Contains(txtNo.Text))
                End If

                If rdolTypeDate.SelectedValue = 1 Then
                    If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                        Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                        Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.TRANSDATE >= strStartDate And tbl_Amlo.TRANSDATE <= strEndDate)
                    End If
                Else

                    If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                        Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                        Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SAVEDATE >= strStartDate And tbl_Amlo.SAVEDATE <= strEndDate)
                    End If
                End If

                If Not (rdolSignType.SelectedValue = 0) Then
                    If (rdolSignType.SelectedValue = 1) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 1)
                    ElseIf (rdolSignType.SelectedValue = 2) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 0)
                    End If

                End If

                If Not (rdolIsSend.SelectedValue = 0) Then
                    If (rdolIsSend.SelectedValue = 1) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 1)
                    ElseIf (rdolIsSend.SelectedValue = 2) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 0)
                    End If

                End If

                'dsAmloXML.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
                'If (qryAmlo.Count = 0) Then
                '    lblMsg.Visible = True
                'Else
                '    lblMsg.Visible = False

                'End If
                dsAmloXML.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
                If (qryAmlo.Count = 0) Then
                    lblMsg.Visible = True
                Else
                    lblMsg.Visible = False
                End If

                Dim AmloXML As New clsAmloXMLShort
                Dim path As String
                Dim iCount, iAllRec As Integer
                Dim CountChk As Integer = 0
                Dim CountChkToGen As Integer = 0
                Dim nAmlo2Data As clsAmlo2Data = New clsAmlo2Data
                Dim nAmlo2Psn(9) As clsAmlo2Psn
                iAllRec = dsAmloXML.Tables("Amlo").Rows.Count

                For iCount = 0 To iAllRec - 1
                    Dim chkConfirmGen As CheckBox = CType(grdAmlo.Rows(iCount).FindControl("CheckBox1"), CheckBox)
                    If (chkConfirmGen.Checked) Then
                        CountChk += 1
                    End If
                Next

                path = "C:\EXPORTXML\AMLO102.xml"
                FileCopy("C:\EXPORTXML\Template\AMLO102.xml", "C:\EXPORTXML\AMLO102.xml")
                For iCount = 0 To iAllRec - 1

                    Dim chkConfirmGen As CheckBox = CType(grdAmlo.Rows(iCount).FindControl("CheckBox1"), CheckBox)
                    If (chkConfirmGen.Checked) Then
                        CountChkToGen += 1


                        'ชนิดของรายงาน 1-01
                        nAmlo2Data.p01_type = "1-02"
                        'เลขที่รายงาน 

                        Dim strRptNo As String = ""

                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("GHBCODE").ToString
                        'strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("DEPTCODE").ToString
                        'strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("BRANCHCODE").ToString.Substring(0, 3)
                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("SOL_ID").ToString
                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTYEAR").ToString
                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTNO").ToString

                        nAmlo2Data.p02_reportid = strRptNo 'dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTNO").ToString
                        'ครั้งที่แก้ไข/เพิ่มเติม  -ถ้าไม่ระบุจะถือเป็นฉบับแรก
                        nAmlo2Data.p03_revision = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTREV").ToString, enmTypedata.typeString)
                        'วันที่รายงาน -วันที่/เดือน/ปี พ.ศ.(4หลัก)
                        nAmlo2Data.p04_date = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTDATE").ToString, enmTypedata.typeDate)
                        '=================================
                        'ส่วนที่ 1. ผู้ทำธุรกรรม 
                        nAmlo2Data.p101_cnd_name_prefix = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("PREFIX").ToString, enmTypedata.typeString)
                        nAmlo2Data.p102_cnd_name_first = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FIRSTNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p103_cnd_name_middle = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("MIDNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p104_cnd_name_last = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("LASTNAME").ToString, enmTypedata.typeString)

                        nAmlo2Data.p105_cnd_id_no = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("UNIQUEID").ToString, enmTypedata.typeString)
                        nAmlo2Data.p106_cnd_id_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("UNIQUETYPE").ToString, enmTypedata.typeString)

                        nAmlo2Data.p107_cnd_location_addr = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FORMALADDR").ToString, enmTypedata.typeString)
                        nAmlo2Data.p108_cnd_location_phone = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FORMALPHONE").ToString, enmTypedata.typeString)
                        nAmlo2Data.p109_cnd_location_fax = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FORMALFAX").ToString, enmTypedata.typeString)


                        'ระบุอาชีพ 1-เจ้าของกิจการ, 2-ข้าราชการ, 3-รับจ้าง, 4-พนักงานรัฐวิสาหกิจ, 5-อาชีพอิสระ, 6-นิสิต นักศึกษา, 99-อื่นๆ
                        nAmlo2Data.p110_cnd_office_occ = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OCCUPATIONTYPE").ToString, enmTypedata.typeString)
                        nAmlo2Data.p111_cnd_office_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OCCUPATIONTYPEOTHER").ToString, enmTypedata.typeString)
                        nAmlo2Data.p112_cnd_office_org = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("WORKADDR").ToString, enmTypedata.typeString)
                        nAmlo2Data.p113_cnd_office_phone = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("WORKPHONE").ToString, enmTypedata.typeString)
                        nAmlo2Data.p114_cnd_office_fax = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("WORKFAX").ToString, enmTypedata.typeString)


                        nAmlo2Data.p115_cnd_contact_addr = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CONTACTADDR").ToString, enmTypedata.typeString)
                        nAmlo2Data.p116_cnd_contact_phone = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CONTACTPHONE").ToString, enmTypedata.typeString)
                        nAmlo2Data.p117_cnd_contact_fax = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CONTACTFAX").ToString, enmTypedata.typeString)


                        'ชนิดหลักฐาน 1-บัตรประจำตัวประชาชน (ต้องเป็นตัวเลขและมีขนาด 13 หลักเท่านั้น), 2-บัตรประจำตัวผู้เสียภาษีอากร (ต้องเป็นตัวเลขและมีขนาด 10 หลักเท่านั้น), 3-หนังสือเดินทาง, 4-ใบสำคัญคนต่างด้าว,
                        ' 5- หนังสือรับรองข้อความในทะเบียน, 6 - บัตรที่ออกโดยองค์กรรัฐ, 7 - หนังสือสุทธิสำหรับพระภิกษุ, 8 -บัตรประจำตัวชนกลุ่มน้อย, 99 - อื่นๆ 
                        'nAmlo2Data.p118_cnd_iddoc_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CUSTIDTYPE").ToString, enmTypedata.typeString)
                        'nAmlo2Data.p119_cnd_iddoc_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CUSTIDTYPEOTHER").ToString, enmTypedata.typeString)
                        'nAmlo2Data.p120_cnd_iddoc_no = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CUSTID").ToString, enmTypedata.typeString)
                        'nAmlo2Data.p121_cnd_iddoc_by = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVBY").ToString, enmTypedata.typeString)

                        'Try
                        '    'nAmlo2Data.p122_cnd_iddoc_date = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGEN").ToString, enmTypedata.typeDate)
                        '    nAmlo2Data.p122_cnd_iddoc_date = setValueDefault(If(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGENTEXT").ToString = "-", dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGENTEXT").ToString, clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGENTEXT").ToString)), enmTypedata.typeother)
                        'Catch ex As Exception

                        'End Try

                        'Try
                        '    ' nAmlo2Data.p123_cnd_iddoc_expire = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRE").ToString, enmTypedata.typeDate)
                        '    nAmlo2Data.p123_cnd_iddoc_expire = setValueDefault(If(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRETEXT").ToString = "-", dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRETEXT").ToString, clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRETEXT").ToString)), enmTypedata.typeother)

                        'Catch ex As Exception

                        'End Try

                        '==================================
                        'ส่วนที่ 3. ข้อเท็จจริงเกี่ยวกับธุรกรรม

                        nAmlo2Data.p301_tsc_date = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSDATE").ToString, enmTypedata.typeDate)
                        nAmlo2Data.p302_tsc_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSTYPE").ToString, enmTypedata.typeString)
                        nAmlo2Data.p303_tsc_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSTYPEOTHER").ToString, enmTypedata.typeString)
                        nAmlo2Data.p304_tsc_asset_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ASSETTYPE").ToString, enmTypedata.typeString)

                        nAmlo2Data.p305_tsc_asset_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ASSETTYPEOTHER").ToString, enmTypedata.typeString)
                        nAmlo2Data.p306_tsc_asset_detail = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ASSETTYPEDETAIL").ToString, enmTypedata.typeString)
                        nAmlo2Data.p307_tsc_asset_amt = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSAMT").ToString, enmTypedata.typeString)
                        nAmlo2Data.p308_tsc_acc_accno = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCOUNTNO").ToString, enmTypedata.typeString)
                        nAmlo2Data.p309_tsc_acc_aname = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCOUNTNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p310_tsc_acc_name_prefix = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCPREFIX").ToString, enmTypedata.typeString)
                        nAmlo2Data.p311_tsc_acc_name_first = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCFIRSTNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p312_tsc_acc_name_middle = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCMIDNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p313_tsc_acc_name_last = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCLASTNAME").ToString, enmTypedata.typeString)

                        nAmlo2Data.p314_tsc_racc_accno = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCOUNTREF").ToString, enmTypedata.typeString)
                        nAmlo2Data.p315_tsc_racc_aname = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCOUNTREFNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p316_tsc_racc_name_prefix = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFPREFIX").ToString, enmTypedata.typeString)
                        nAmlo2Data.p317_tsc_racc_name_first = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFFIRSTNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p318_tsc_racc_name_middle = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFMIDNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p319_tsc_racc_name_last = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFLASTNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p320_tsc_racc_relation_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFERENCETYPE").ToString, enmTypedata.typeString)
                        nAmlo2Data.p321_tsc_racc_relation_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFERENCETYPEOTHER").ToString, enmTypedata.typeString)
                        nAmlo2Data.p322_tsc_name_prefix = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINPREFIX").ToString, enmTypedata.typeString)
                        nAmlo2Data.p323_tsc_name_first = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINFIRSTNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p324_tsc_name_middle = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINMIDNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p325_tsc_name_last = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINLASTNAME").ToString, enmTypedata.typeString)
                        nAmlo2Data.p326_tsc_obj_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OBJBIZ").ToString, enmTypedata.typeString)
                        nAmlo2Data.p327_tsc_obj_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OBJBIZOTHER").ToString, enmTypedata.typeString)
                        nAmlo2Data.p401_record_writer = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("SAVETYPE").ToString, enmTypedata.typeString)
                        nAmlo2Data.p402_record_sign = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("SIGNTYPE").ToString, enmTypedata.typeString)
                        nAmlo2Data.p403_record_date = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("SAVEDATE").ToString, enmTypedata.typeDate)



                        Dim mRptSeq As Integer = Convert.ToInt32(dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTSEQ").ToString)
                        Dim qryAmloDetailCount = From tblAmloDetail In AMLO102Entity.AMLODETAIL102
                         Where tblAmloDetail.RPTSEQ = mRptSeq And tblAmloDetail.RPTIDX > 1
                         Group By tblAmloDetail.RPTSEQ Into MaxDetail = Max(tblAmloDetail.RPTIDX)

                        dsAmloXMLCount = New DataSet()
                        '    NewObjAmloDetail()
                        dsAmloXMLCount.Tables.Add(clsDataTable.LINQToDataTable(qryAmloDetailCount, "AmloCount").Copy)


                        Dim iPsnCount As Integer = 0
                        If dsAmloXMLCount.Tables("AmloCount").Rows.Count > 0 Then
                            Dim mMaxRptDetail As Integer = Convert.ToInt32(dsAmloXMLCount.Tables("AmloCount").Rows(0).Item("MaxDetail").ToString)
                            Dim qryAmloDetail = From tblAmloDetail In AMLO102Entity.AMLODETAIL102
                            Where tblAmloDetail.RPTSEQ = mRptSeq And tblAmloDetail.RPTIDX > 1
                            Order By tblAmloDetail.RPTIDX
                             Select tblAmloDetail.EVBY, tblAmloDetail.EVGEN, tblAmloDetail.EVEXPIRE, tblAmloDetail.CUSTIDTYPE, tblAmloDetail.CUSTIDTYPEOTHER, _
                                tblAmloDetail.CUSTID, tblAmloDetail.OCCUPATIONTYPE, tblAmloDetail.OCCUPATIONTYPEOTHER, tblAmloDetail.CONTACTADDR, _
                                 tblAmloDetail.CONTACTPHONE, tblAmloDetail.CONTACTFAX, tblAmloDetail.FORMALADDR, tblAmloDetail.FORMALPHONE, tblAmloDetail.BIZTYPE, _
                            tblAmloDetail.FORMALFAX, tblAmloDetail.WORKADDR, tblAmloDetail.WORKPHONE, tblAmloDetail.WORKFAX, tblAmloDetail.RPTIDXTYPE, _
                            tblAmloDetail.PREFIX, tblAmloDetail.FIRSTNAME, tblAmloDetail.MIDNAME, tblAmloDetail.LASTNAME, tblAmloDetail.UNIQUETYPE, tblAmloDetail.UNIQUEID

                            dsAmloXMLDetail = New DataSet()
                            dsAmloXMLDetail.Tables.Add(clsDataTable.LINQToDataTable(qryAmloDetail, "AmloDetail").Copy)


                            For Each dr As DataRow In dsAmloXMLDetail.Tables("AmloDetail").Rows
                                iPsnCount = iPsnCount + 1
                                nAmlo2Psn(iPsnCount) = New clsAmlo2Psn(iPsnCount)


                                nAmlo2Psn(iPsnCount).p201_psn_relation = setValueDefault(dr("RPTIDXTYPE").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p202_psn_name_prefix = setValueDefault(dr("PREFIX").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p203_psn_name_first = setValueDefault(dr("FIRSTNAME").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p204_psn_name_middle = setValueDefault(dr("MIDNAME").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p205_psn_name_last = setValueDefault(dr("LASTNAME").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p206_psn_id_no = setValueDefault(dr("UNIQUEID").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p207_psn_id_type = setValueDefault(dr("UNIQUETYPE").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p208_psn_location_addr = setValueDefault(dr("FORMALADDR").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p209_psn_location_phone = setValueDefault(dr("FORMALPHONE").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p210_psn_location_fax = setValueDefault(dr("FORMALFAX").ToString, enmTypedata.typeString)


                                'ระบุอาชีพ 1-เจ้าของกิจการ, 2-ข้าราชการ, 3-รับจ้าง, 4-พนักงานรัฐวิสาหกิจ, 5-อาชีพอิสระ, 6-นิสิต นักศึกษา, 99-อื่นๆ
                                nAmlo2Psn(iPsnCount).p211_psn_office_occ = setValueDefault(dr("OCCUPATIONTYPE").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p212_psn_office_org = setValueDefault(dr("WORKADDR").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p213_psn_office_phone = setValueDefault(dr("WORKPHONE").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p214_psn_office_fax = setValueDefault(dr("WORKFAX").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p215_psn_office_type = setValueDefault(dr("BIZTYPE").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p216_psn_office_desc = setValueDefault(dr("OCCUPATIONTYPEOTHER").ToString, enmTypedata.typeString)

                                nAmlo2Psn(iPsnCount).p217_psn_contact_addr = setValueDefault(dr("CONTACTADDR").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p218_psn_contact_phone = setValueDefault(dr("CONTACTPHONE").ToString, enmTypedata.typeString)
                                nAmlo2Psn(iPsnCount).p219_psn_contact_fax = setValueDefault(dr("CONTACTFAX").ToString, enmTypedata.typeString)

                                'ชนิดหลักฐาน 1-บัตรประจำตัวประชาชน (ต้องเป็นตัวเลขและมีขนาด 13 หลักเท่านั้น), 2-บัตรประจำตัวผู้เสียภาษีอากร (ต้องเป็นตัวเลขและมีขนาด 10 หลักเท่านั้น), 3-หนังสือเดินทาง, 4-ใบสำคัญคนต่างด้าว,
                                ' 5- หนังสือรับรองข้อความในทะเบียน, 6 - บัตรที่ออกโดยองค์กรรัฐ, 7 - หนังสือสุทธิสำหรับพระภิกษุ, 8 -บัตรประจำตัวชนกลุ่มน้อย, 99 - อื่นๆ 

                                'nAmlo2Psn(iPsnCount).p220_psn_iddoc_type = setValueDefault(dr("CUSTIDTYPE").ToString, enmTypedata.typeString)
                                'nAmlo2Psn(iPsnCount).p221_psn_iddoc_desc = setValueDefault(dr("CUSTIDTYPEOTHER").ToString, enmTypedata.typeString)
                                'nAmlo2Psn(iPsnCount).p222_psn_iddoc_no = setValueDefault(dr("CUSTID").ToString, enmTypedata.typeString)
                                'nAmlo2Psn(iPsnCount).p223_psn_iddoc_by = setValueDefault(dr("EVBY").ToString, enmTypedata.typeString)

                                'Try
                                '    'nAmlo2Psn(iPsnCount).p224_psn_iddoc_date = setValueDefault(dr("EVGEN").ToString, enmTypedata.typeDate)
                                '    nAmlo2Psn(iPsnCount).p224_psn_iddoc_date = setValueDefault(If(dr("EVGENTEXT").ToString = "-", dr("EVGENTEXT").ToString, clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dr("EVGENTEXT").ToString)), enmTypedata.typeother)

                                'Catch ex As Exception

                                'End Try
                                'Try
                                '    ' nAmlo2Psn(iPsnCount).p225_psn_iddoc_expire = setValueDefault(dr("EVEXPIRE").ToString, enmTypedata.typeDate)
                                '    nAmlo2Psn(iPsnCount).p225_psn_iddoc_expire = setValueDefault(If(dr("EVEXPIRETEXT").ToString = "-", dr("EVEXPIRETEXT").ToString, clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dr("EVEXPIRETEXT").ToString)), enmTypedata.typeother)
                                'Catch ex As Exception

                                'End Try

                            Next
                        End If
                        If Not (AmloXML.AddAMLO2node(path, nAmlo2Data, iPsnCount, nAmlo2Psn, CountChkToGen, CountChk)) Then
                            Exit Sub
                        End If


                    End If
                Next

                DownloadFile("C:\ExportXML\AMLO102.xml", "AMLO102.xml", True)
                ClearObj()


            ElseIf (ddlRrptType.SelectedValue = "M53") Then
                Dim qryAmlo = From tblAmlo In AMLO103Entity.AMLO103 Join tblAmloDetail In AMLO103Entity.AMLODETAIL103 On tblAmlo.RPTSEQ Equals tblAmloDetail.RPTSEQ
                      Where tblAmloDetail.RPTIDX = 1 And tblAmlo.ISCOMPLETE = 1 _
                      Order By tblAmlo.RPTSEQ Descending, tblAmlo.DEPTCODE Descending, tblAmlo.BRANCHCODE Descending _
                      Select tblAmlo.RPTSEQ, tblAmlo.GHBCODE, tblAmlo.RPTTYPE, tblAmlo.RPTYEAR, tblAmlo.RPTNO, _
                        tblAmlo.RPTREV, tblAmlo.BRANCHCODE, tblAmlo.SOL_ID, tblAmlo.DEPTCODE, tblAmlo.RPTDATE, tblAmlo.TRANSDATE, _
                        tblAmlo.TRANSTYPE, tblAmlo.TRANSTYPEOTHER, tblAmlo.TRANSAMT, tblAmlo.TRANSAMTCHAR, _
                        tblAmlo.CURRENCY, tblAmlo.ACCOUNTNO, tblAmlo.ACCOUNTNAME, tblAmlo.ACCPREFIX, tblAmlo.ACCFIRSTNAME, _
                        tblAmlo.ACCMIDNAME, tblAmlo.ACCLASTNAME, tblAmlo.ACCOUNTREF, tblAmlo.ACCOUNTREFNAME, tblAmlo.REFPREFIX, _
                        tblAmlo.REFFIRSTNAME, tblAmlo.REFMIDNAME, tblAmlo.REFLASTNAME, tblAmlo.REFERENCETYPE, tblAmlo.REFERENCETYPEOTHER, _
                        tblAmlo.GAINPREFIX, tblAmlo.GAINFIRSTNAME, tblAmlo.GAINMIDNAME, tblAmlo.GAINLASTNAME, tblAmlo.OBJBIZ, _
                        tblAmlo.OBJBIZOTHER, tblAmlo.SAVETYPE, tblAmlo.SAVEDATE, tblAmlo.FINANORGCODE, tblAmlo.RESON, tblAmlo.ISSEND, tblAmlo.DOCREF, _
                        tblAmloDetail.RPTIDX, tblAmloDetail.RPTIDXTYPE, tblAmloDetail.UNIQUETYPE, tblAmloDetail.UNIQUEID, _
                        tblAmloDetail.PREFIX, tblAmloDetail.FIRSTNAME, tblAmloDetail.MIDNAME, tblAmloDetail.LASTNAME, _
                        tblAmloDetail.FORMALADDR, tblAmloDetail.FORMALPHONE, tblAmloDetail.FORMALFAX, tblAmloDetail.OCCUPATIONTYPE, _
                        tblAmloDetail.OCCUPATIONTYPEOTHER, tblAmloDetail.WORKADDR, tblAmloDetail.WORKPHONE, tblAmloDetail.WORKFAX, _
                        tblAmloDetail.BIZTYPE, tblAmloDetail.BIZTYPEOTHER, tblAmloDetail.CONTACTADDR, tblAmloDetail.CONTACTPHONE, _
                        tblAmloDetail.CONTACTFAX, tblAmloDetail.CUSTIDTYPE, tblAmloDetail.CUSTIDTYPEOTHER, tblAmloDetail.CUSTID, _
                        tblAmloDetail.EVBY, tblAmloDetail.EVGEN, tblAmloDetail.EVGENTEXT, tblAmloDetail.EVEXPIRE, tblAmloDetail.EVEXPIRETEXT

                If Not (ddlDepartment.SelectedValue = "0") Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = ddlDepartment.SelectedValue)
                End If

                If Not (ddlBranch.SelectedIndex = 0) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.BRANCHCODE.Contains(ddlBranch.SelectedValue))
                End If

                'If Not (txtYear.Text = "") Then
                '    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTYEAR.Value = CInt(txtYear.Text))
                'End If
                If Not (txtNo.Text = "") Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTNO.Contains(txtNo.Text))
                End If

                If rdolTypeDate.SelectedValue = 1 Then
                    If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                        Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                        Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.TRANSDATE >= strStartDate And tbl_Amlo.TRANSDATE <= strEndDate)
                    End If
                Else

                    If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                        Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text)
                        Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text)
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SAVEDATE >= strStartDate And tbl_Amlo.SAVEDATE <= strEndDate)
                    End If
                End If

                'If Not (rdolSignType.SelectedValue = 0) Then
                '    If (rdolSignType.SelectedValue = 1) Then
                '        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 1)
                '    ElseIf (rdolSignType.SelectedValue = 2) Then
                '        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.SIGNTYPE = 0)
                '    End If

                'End If

                If Not (rdolIsSend.SelectedValue = 0) Then
                    If (rdolIsSend.SelectedValue = 1) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 1)
                    ElseIf (rdolIsSend.SelectedValue = 2) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 0)
                    End If

                End If

                'dsAmloXML.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
                'If (qryAmlo.Count = 0) Then
                '    lblMsg.Visible = True
                'Else
                '    lblMsg.Visible = False

                'End If
                dsAmloXML.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
                If (qryAmlo.Count = 0) Then
                    lblMsg.Visible = True
                Else
                    lblMsg.Visible = False
                End If

                Dim AmloXML As New clsAmloXMLShort
                Dim path As String
                Dim iCount, iAllRec As Integer
                Dim CountChk As Integer = 0
                Dim CountChkToGen As Integer = 0
                Dim nAmlo3Data As clsAmlo3Data = New clsAmlo3Data
                Dim nAmlo3Psn(9) As clsAmlo3Psn
                iAllRec = dsAmloXML.Tables("Amlo").Rows.Count

                For iCount = 0 To iAllRec - 1
                    Dim chkConfirmGen As CheckBox = CType(grdAmlo.Rows(iCount).FindControl("CheckBox1"), CheckBox)
                    If (chkConfirmGen.Checked) Then
                        CountChk += 1
                    End If
                Next

                path = "C:\EXPORTXML\AMLO103.xml"
                FileCopy("C:\EXPORTXML\Template\AMLO103.xml", "C:\EXPORTXML\AMLO103.xml")
                For iCount = 0 To iAllRec - 1

                    Dim chkConfirmGen As CheckBox = CType(grdAmlo.Rows(iCount).FindControl("CheckBox1"), CheckBox)
                    If (chkConfirmGen.Checked) Then
                        CountChkToGen += 1



                        'ชนิดของรายงาน 1-01
                        nAmlo3Data.p01_type = "1-03"
                        'เลขที่รายงาน 
                        Dim strRptNo As String = ""

                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("GHBCODE").ToString
                        'strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("DEPTCODE").ToString
                        'strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("BRANCHCODE").ToString.Substring(0, 3)
                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("SOL_ID").ToString
                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTYEAR").ToString
                        strRptNo = strRptNo & dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTNO").ToString

                        nAmlo3Data.p02_reportid = strRptNo
                        'ครั้งที่แก้ไข/เพิ่มเติม  -ถ้าไม่ระบุจะถือเป็นฉบับแรก
                        nAmlo3Data.p03_revision = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTREV").ToString, enmTypedata.typeString)
                        'วันที่รายงาน -วันที่/เดือน/ปี พ.ศ.(4หลัก)
                        nAmlo3Data.p04_date = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTDATE").ToString, enmTypedata.typeDate)
                        'ประกอบรายงานเลขที่
                        nAmlo3Data.p05_refer = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("DOCREF").ToString, enmTypedata.typeString)
                        '=================================
                        'ส่วนที่ 1. ผู้ทำธุรกรรม 
                        nAmlo3Data.p101_cnd_name_prefix = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("PREFIX").ToString, enmTypedata.typeString)
                        nAmlo3Data.p102_cnd_name_first = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FIRSTNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p103_cnd_name_middle = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("MIDNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p104_cnd_name_last = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("LASTNAME").ToString, enmTypedata.typeString)

                        nAmlo3Data.p105_cnd_id_no = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("UNIQUEID").ToString, enmTypedata.typeString)
                        nAmlo3Data.p106_cnd_id_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("UNIQUETYPE").ToString, enmTypedata.typeString)

                        nAmlo3Data.p107_cnd_location_addr = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FORMALADDR").ToString, enmTypedata.typeString)
                        nAmlo3Data.p108_cnd_location_phone = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FORMALPHONE").ToString, enmTypedata.typeString)
                        nAmlo3Data.p109_cnd_location_fax = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("FORMALFAX").ToString, enmTypedata.typeString)


                        'ระบุอาชีพ 1-เจ้าของกิจการ, 2-ข้าราชการ, 3-รับจ้าง, 4-พนักงานรัฐวิสาหกิจ, 5-อาชีพอิสระ, 6-นิสิต นักศึกษา, 99-อื่นๆ
                        nAmlo3Data.p110_cnd_office_occ = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OCCUPATIONTYPE").ToString, enmTypedata.typeString)
                        nAmlo3Data.p111_cnd_office_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OCCUPATIONTYPEOTHER").ToString, enmTypedata.typeString)
                        nAmlo3Data.p112_cnd_office_org = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("WORKADDR").ToString, enmTypedata.typeString)
                        nAmlo3Data.p113_cnd_office_phone = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("WORKPHONE").ToString, enmTypedata.typeString)
                        nAmlo3Data.p114_cnd_office_fax = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("WORKFAX").ToString, enmTypedata.typeString)


                        nAmlo3Data.p115_cnd_contact_addr = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CONTACTADDR").ToString, enmTypedata.typeString)
                        nAmlo3Data.p116_cnd_contact_phone = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CONTACTPHONE").ToString, enmTypedata.typeString)
                        nAmlo3Data.p117_cnd_contact_fax = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CONTACTFAX").ToString, enmTypedata.typeString)


                        'ชนิดหลักฐาน 1-บัตรประจำตัวประชาชน (ต้องเป็นตัวเลขและมีขนาด 13 หลักเท่านั้น), 2-บัตรประจำตัวผู้เสียภาษีอากร (ต้องเป็นตัวเลขและมีขนาด 10 หลักเท่านั้น), 3-หนังสือเดินทาง, 4-ใบสำคัญคนต่างด้าว,
                        ' 5- หนังสือรับรองข้อความในทะเบียน, 6 - บัตรที่ออกโดยองค์กรรัฐ, 7 - หนังสือสุทธิสำหรับพระภิกษุ, 8 -บัตรประจำตัวชนกลุ่มน้อย, 99 - อื่นๆ 
                        nAmlo3Data.p118_cnd_iddoc_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CUSTIDTYPE").ToString, enmTypedata.typeString)
                        nAmlo3Data.p119_cnd_iddoc_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CUSTIDTYPEOTHER").ToString, enmTypedata.typeString)
                        nAmlo3Data.p120_cnd_iddoc_no = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CUSTID").ToString, enmTypedata.typeString)
                        nAmlo3Data.p121_cnd_iddoc_by = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVBY").ToString, enmTypedata.typeString)

                        Try
                            'nAmlo3Data.p122_cnd_iddoc_date = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGEN").ToString, enmTypedata.typeDate)
                            nAmlo3Data.p122_cnd_iddoc_date = setValueDefault(If(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGENTEXT").ToString = "-", dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGENTEXT").ToString,
                                                                        clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVGENTEXT").ToString)), enmTypedata.typeother)

                        Catch ex As Exception

                        End Try
                        Try
                            ' nAmlo3Data.p123_cnd_iddoc_expire = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRE").ToString, enmTypedata.typeDate)
                            nAmlo3Data.p123_cnd_iddoc_expire = setValueDefault(If(dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRETEXT").ToString = "-", dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRETEXT").ToString,
                                                                                  clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dsAmloXML.Tables("Amlo").Rows(iCount).Item("EVEXPIRETEXT").ToString)), enmTypedata.typeother)

                        Catch ex As Exception

                        End Try

                        'ส่วนที่ 3. ข้อเท็จจริงเกี่ยวกับธุรกรรม
                        nAmlo3Data.p301_tsc_date = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSDATE").ToString, enmTypedata.typeString)
                        nAmlo3Data.p302_tsc_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSTYPE").ToString, enmTypedata.typeString)
                        nAmlo3Data.p303_tsc_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSTYPEOTHER").ToString, enmTypedata.typeString)

                        nAmlo3Data.p304_tsc_cur = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("CURRENCY").ToString, enmTypedata.typeString)
                        nAmlo3Data.p305_tsc_amt = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("TRANSAMT").ToString, enmTypedata.typeString)
                        nAmlo3Data.p306_tsc_acc_accno = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCOUNTNO").ToString, enmTypedata.typeString)
                        nAmlo3Data.p307_tsc_acc_aname = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCOUNTNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p308_tsc_acc_name_prefix = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCPREFIX").ToString, enmTypedata.typeString)
                        nAmlo3Data.p309_tsc_acc_name_first = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCFIRSTNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p310_tsc_acc_name_middle = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCMIDNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p311_tsc_acc_name_last = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCLASTNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p312_tsc_racc_accno = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCOUNTREF").ToString, enmTypedata.typeString)
                        nAmlo3Data.p313_tsc_racc_aname = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("ACCOUNTREFNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p314_tsc_racc_name_prefix = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFPREFIX").ToString, enmTypedata.typeString)
                        nAmlo3Data.p315_tsc_racc_name_first = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFFIRSTNAME").ToString, enmTypedata.typeString)

                        nAmlo3Data.p316_tsc_racc_name_middle = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFMIDNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p317_tsc_racc_name_last = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFLASTNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p318_tsc_racc_relation_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFERENCETYPE").ToString, enmTypedata.typeString)
                        nAmlo3Data.p319_tsc_racc_relation_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("REFERENCETYPEOTHER").ToString, enmTypedata.typeString)
                        nAmlo3Data.p320_tsc_name_prefix = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINPREFIX").ToString, enmTypedata.typeString)
                        nAmlo3Data.p321_tsc_name_first = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINFIRSTNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p322_tsc_name_middle = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINMIDNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p323_tsc_name_last = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("GAINLASTNAME").ToString, enmTypedata.typeString)
                        nAmlo3Data.p324_tsc_obj_type = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OBJBIZ").ToString, enmTypedata.typeString)
                        nAmlo3Data.p325_tsc_obj_desc = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("OBJBIZOTHER").ToString, enmTypedata.typeString)
                        nAmlo3Data.p401_record_note = setValueDefault(dsAmloXML.Tables("Amlo").Rows(iCount).Item("RESON").ToString, enmTypedata.typeString)




                        Dim mRptSeq As Integer = Convert.ToInt32(dsAmloXML.Tables("Amlo").Rows(iCount).Item("RPTSEQ").ToString)
                        Dim qryAmloDetailCount = From tblAmloDetail In AMLO103Entity.AMLODETAIL103
                         Where tblAmloDetail.RPTSEQ = mRptSeq And tblAmloDetail.RPTIDX > 1
                         Group By tblAmloDetail.RPTSEQ Into MaxDetail = Max(tblAmloDetail.RPTIDX)

                        dsAmloXMLCount = New DataSet()
                        '    NewObjAmloDetail()
                        dsAmloXMLCount.Tables.Add(clsDataTable.LINQToDataTable(qryAmloDetailCount, "AmloCount").Copy)


                        Dim iPsnCount As Integer = 0
                        If dsAmloXMLCount.Tables("AmloCount").Rows.Count > 0 Then
                            Dim mMaxRptDetail As Integer = Convert.ToInt32(dsAmloXMLCount.Tables("AmloCount").Rows(0).Item("MaxDetail").ToString)
                            Dim qryAmloDetail = From tblAmloDetail In AMLO103Entity.AMLODETAIL103
                            Where tblAmloDetail.RPTSEQ = mRptSeq And tblAmloDetail.RPTIDX > 1
                            Order By tblAmloDetail.RPTIDX
                             Select tblAmloDetail.EVBY, tblAmloDetail.EVGEN, tblAmloDetail.EVEXPIRE, tblAmloDetail.CUSTIDTYPE, tblAmloDetail.CUSTIDTYPEOTHER, _
                                tblAmloDetail.CUSTID, tblAmloDetail.OCCUPATIONTYPE, tblAmloDetail.OCCUPATIONTYPEOTHER, tblAmloDetail.CONTACTADDR, _
                                 tblAmloDetail.CONTACTPHONE, tblAmloDetail.CONTACTFAX, tblAmloDetail.FORMALADDR, tblAmloDetail.FORMALPHONE, tblAmloDetail.BIZTYPE, _
                            tblAmloDetail.FORMALFAX, tblAmloDetail.WORKADDR, tblAmloDetail.WORKPHONE, tblAmloDetail.WORKFAX, tblAmloDetail.RPTIDXTYPE, _
                            tblAmloDetail.PREFIX, tblAmloDetail.FIRSTNAME, tblAmloDetail.MIDNAME, tblAmloDetail.LASTNAME, tblAmloDetail.UNIQUETYPE, tblAmloDetail.UNIQUEID

                            dsAmloXMLDetail = New DataSet()
                            dsAmloXMLDetail.Tables.Add(clsDataTable.LINQToDataTable(qryAmloDetail, "AmloDetail").Copy)


                            For Each dr As DataRow In dsAmloXMLDetail.Tables("AmloDetail").Rows
                                iPsnCount = iPsnCount + 1
                                nAmlo3Psn(iPsnCount) = New clsAmlo3Psn(iPsnCount)


                                nAmlo3Psn(iPsnCount).p201_psn_relation = setValueDefault(dr("RPTIDXTYPE").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p202_psn_name_prefix = setValueDefault(dr("PREFIX").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p203_psn_name_first = setValueDefault(dr("FIRSTNAME").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p204_psn_name_middle = setValueDefault(dr("MIDNAME").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p205_psn_name_last = setValueDefault(dr("LASTNAME").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p206_psn_id_no = setValueDefault(dr("UNIQUEID").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p207_psn_id_type = setValueDefault(dr("UNIQUETYPE").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p208_psn_location_addr = setValueDefault(dr("FORMALADDR").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p209_psn_location_phone = setValueDefault(dr("FORMALPHONE").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p210_psn_location_fax = setValueDefault(dr("FORMALFAX").ToString, enmTypedata.typeString)


                                'ระบุอาชีพ 1-เจ้าของกิจการ, 2-ข้าราชการ, 3-รับจ้าง, 4-พนักงานรัฐวิสาหกิจ, 5-อาชีพอิสระ, 6-นิสิต นักศึกษา, 99-อื่นๆ
                                nAmlo3Psn(iPsnCount).p211_psn_office_occ = setValueDefault(dr("OCCUPATIONTYPE").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p212_psn_office_org = setValueDefault(dr("WORKADDR").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p213_psn_office_phone = setValueDefault(dr("WORKPHONE").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p214_psn_office_fax = setValueDefault(dr("WORKFAX").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p215_psn_office_type = setValueDefault(dr("BIZTYPE").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p216_psn_office_desc = setValueDefault(dr("OCCUPATIONTYPEOTHER").ToString, enmTypedata.typeString)

                                nAmlo3Psn(iPsnCount).p217_psn_contact_addr = setValueDefault(dr("CONTACTADDR").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p218_psn_contact_phone = setValueDefault(dr("CONTACTPHONE").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p219_psn_contact_fax = setValueDefault(dr("CONTACTFAX").ToString, enmTypedata.typeString)

                                'ชนิดหลักฐาน 1-บัตรประจำตัวประชาชน (ต้องเป็นตัวเลขและมีขนาด 13 หลักเท่านั้น), 2-บัตรประจำตัวผู้เสียภาษีอากร (ต้องเป็นตัวเลขและมีขนาด 10 หลักเท่านั้น), 3-หนังสือเดินทาง, 4-ใบสำคัญคนต่างด้าว,
                                ' 5- หนังสือรับรองข้อความในทะเบียน, 6 - บัตรที่ออกโดยองค์กรรัฐ, 7 - หนังสือสุทธิสำหรับพระภิกษุ, 8 -บัตรประจำตัวชนกลุ่มน้อย, 99 - อื่นๆ 

                                nAmlo3Psn(iPsnCount).p220_psn_iddoc_type = setValueDefault(dr("CUSTIDTYPE").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p221_psn_iddoc_desc = setValueDefault(dr("CUSTIDTYPEOTHER").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p222_psn_iddoc_no = setValueDefault(dr("CUSTID").ToString, enmTypedata.typeString)
                                nAmlo3Psn(iPsnCount).p223_psn_iddoc_by = setValueDefault(dr("EVBY").ToString, enmTypedata.typeString)
                                Try
                                    ' nAmlo3Psn(iPsnCount).p224_psn_iddoc_date = setValueDefault(dr("EVGEN").ToString, enmTypedata.typeDate)
                                    nAmlo3Psn(iPsnCount).p224_psn_iddoc_date = setValueDefault(If(dr("EVGENTEXT").ToString = "-", dr("EVGENTEXT").ToString, clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dr("EVGENTEXT").ToString)), enmTypedata.typeother)
                                Catch ex As Exception

                                End Try
                                Try
                                    'nAmlo3Psn(iPsnCount).p225_psn_iddoc_expire = setValueDefault(dr("EVEXPIRE").ToString, enmTypedata.typeDate)
                                    nAmlo3Psn(iPsnCount).p225_psn_iddoc_expire = setValueDefault(If(dr("EVEXPIRETEXT").ToString = "-", dr("EVEXPIRETEXT").ToString, clsFormatDate.convertIntToDate(clsDateFormat.enmDateCultureInfo.Th, dr("EVEXPIRETEXT").ToString)), enmTypedata.typeother)

                                Catch ex As Exception

                                End Try
                            Next
                        End If



                        If Not (AmloXML.AddAMLO3node(path, nAmlo3Data, iPsnCount, nAmlo3Psn, CountChkToGen, CountChk)) Then
                            Exit Sub
                        End If
                    End If
                Next

                DownloadFile("C:\ExportXML\AMLO103.xml", "AMLO103.xml", True)
                ClearObj()

            End If



        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                              UpdatePanel1.GetType(), _
                                                              "clientScript", _
                                                              "alert('" + ex.Message.Replace("'", "").Replace("""", "") + "')", _
                                                              True)
        End Try
    End Sub

    Protected Sub btnPrintFull_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrintFull.Click
        Dim param As String = "{ 'RPTTYPE' : '" & ddlRrptType.SelectedValue & "'," _
                                     & "'TRANSTYPE' : '" & ddlTransType.SelectedValue & "'," _
                                     & "'DEPTCODE' : '" & ddlDepartment.SelectedValue & "'," _
                                     & "'BRANCHCODE' : '" & ddlBranch.SelectedValue & "'," _
                                     & "'RPTNO' : '" & txtNo.Text.Trim & "'," _
                                     & "'TYPEDATE' : '" & rdolTypeDate.SelectedValue & "'," _
                                     & "'STARTDATE' : '" & txtDateStart.Text.Trim & "'," _
                                     & "'ENDDATE' : '" & txtDateEnd.Text.Trim & "'," _
                                     & "'SIGNTYPE' : '" & If(rdolSignType.SelectedValue = 0, "", rdolSignType.SelectedValue) & "'," _
                                     & "'ISSEND' : '" & If(rdolIsSend.SelectedValue = 0, "", rdolIsSend.SelectedValue) & "'}"
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType, "key", _
               "OpenWindowWithPost('../Form/frmReportAmlo.aspx','width=730,height=345,left=100,top=100,resizable=yes,scrollbars=yes','Report', " + param + ");", True)
    End Sub

    Protected Sub btnPrint1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint1.Click
        Dim param As String = "{ 'RPTTYPE' : '" & ddlRrptType.SelectedValue & "'," _
                                     & "'TRANSTYPE' : '" & ddlTransType.SelectedValue & "'," _
                                     & "'DEPTCODE' : '" & ddlDepartment.SelectedValue & "'," _
                                     & "'BRANCHCODE' : '" & ddlBranch.SelectedValue & "'," _
                                     & "'RPTNO' : '" & txtNo.Text.Trim & "'," _
                                     & "'TYPEDATE' : '" & rdolTypeDate.SelectedValue & "'," _
                                     & "'STARTDATE' : '" & txtDateStart.Text.Trim & "'," _
                                     & "'ENDDATE' : '" & txtDateEnd.Text.Trim & "'," _
                                     & "'SIGNTYPE' : '" & If(rdolSignType.SelectedValue = 0, "", rdolSignType.SelectedValue) & "'," _
                                     & "'ISSEND' : '" & If(rdolIsSend.SelectedValue = 0, "", rdolIsSend.SelectedValue) & "'," _
                                     & "'RPTSUBTYPE' : '1'}"
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType, "key", _
               "OpenWindowWithPost('../Form/frmReportAmlo.aspx','width=730,height=345,left=100,top=100,resizable=yes,scrollbars=yes','Report', " + param + ");", True)
    End Sub

    Protected Sub btnPrint2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint2.Click
        Dim param As String = "{ 'RPTTYPE' : '" & ddlRrptType.SelectedValue & "'," _
                                     & "'TRANSTYPE' : '" & ddlTransType.SelectedValue & "'," _
                                     & "'DEPTCODE' : '" & ddlDepartment.SelectedValue & "'," _
                                     & "'BRANCHCODE' : '" & ddlBranch.SelectedValue & "'," _
                                     & "'RPTNO' : '" & txtNo.Text.Trim & "'," _
                                     & "'TYPEDATE' : '" & rdolTypeDate.SelectedValue & "'," _
                                     & "'STARTDATE' : '" & txtDateStart.Text.Trim & "'," _
                                     & "'ENDDATE' : '" & txtDateEnd.Text.Trim & "'," _
                                     & "'SIGNTYPE' : '" & If(rdolSignType.SelectedValue = 0, "", rdolSignType.SelectedValue) & "'," _
                                     & "'ISSEND' : '" & If(rdolIsSend.SelectedValue = 0, "", rdolIsSend.SelectedValue) & "'," _
                                     & "'RPTSUBTYPE' : '2'}"
        ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType, "key", _
               "OpenWindowWithPost('../Form/frmReportAmlo.aspx','width=730,height=345,left=100,top=100,resizable=yes,scrollbars=yes','Report', " + param + ");", True)
    End Sub

    Protected Sub btnGenCtlFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGenCtlFile.Click
        Dim strFilename As String = System.IO.Path.GetRandomFileName
        Dim strFriendlyName As String
        Dim CurrentCF = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath)
        Dim REPORTERUSERNAME As String
        Dim EDIUSER As String

        REPORTERUSERNAME = CurrentCF.AppSettings.Settings("REPORTERUSERNAME").Value
        EDIUSER = CurrentCF.AppSettings.Settings("EDIUSER").Value
        strFriendlyName = "AMLO09" & Now.Date.ToString("yyyyMMdd") & ".ctrl"
        Using sw As New System.IO.StreamWriter(Server.MapPath("~\TempFile\" & strFilename + ".ctrl"))
            sw.WriteLine("_REF=AMLO033" & Now.Date.ToString("yyyyMMdd"))
            sw.WriteLine("_INVOICE=AMLO09" & Now.Date.ToString("yyyyMMdd"))
            sw.WriteLine("_EDIUSER=" & EDIUSER)
            sw.WriteLine("_ENCODETYPE=0")
            sw.WriteLine("_ITEMNUMBER=1")
            sw.WriteLine("PAYLOAD_NAME=" & "AMLO09" & Now.Date.ToString("yyyyMMdd") & ".xml")
            sw.WriteLine("PAYLOAD_TYPE=ERS09")
            sw.WriteLine("REPORTERUSERNAME=" & REPORTERUSERNAME)
            sw.WriteLine("SENDEMAILTOREPROTER=TRUE")
            sw.WriteLine("*Start-Message*")
            sw.Close()
        End Using

        Dim fs As System.IO.FileStream = Nothing

        fs = System.IO.File.Open(Server.MapPath("~\TempFile\" & strFilename + ".ctrl"), IO.FileMode.Open)

        Dim btFile(fs.Length) As Byte
        fs.Read(btFile, 0, fs.Length)
        fs.Close()

        Dim TheFile As FileInfo = New FileInfo(Server.MapPath("~\TempFile\" & strFilename + ".ctrl"))
        If TheFile.Exists Then
            File.Delete(Server.MapPath("~\TempFile\" & strFilename + ".ctrl"))
        End If

        With Response
            .AddHeader("Content-Disposition", "attachment; filename=" & strFriendlyName)
            .ContentType = "application/txt"
            .Charset = "UTF-8"
            .BinaryWrite(btFile)
            .End()
        End With
    End Sub

    Protected Sub btnGenXMLNoACID_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGenXMLNoACID.Click
        Dim dsAmloXML As DataSet
        clsFormatDate = New clsDateFormat

        Try
            ' ========================================
            If (ddlRrptType.SelectedValue = "M57") Then
                If (grdAmlo1059.Rows.Count = 0) Then
                    ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                             UpdatePanel1.GetType(), _
                                                             "clientScript", _
                                                             "alert('กรุณาเลือกข้อมูลที่ต้องการออกไฟล์ Xml ')", _
                                                             True)
                    Exit Sub
                End If
            End If

            clsDataTable = New clsLINQToDataTable
            clsFormatDate = New clsDateFormat
            Dim UserRight As clsSessionMenuRight
            UserRight = Session("clsMenuRight")

            dsAmloXML = New DataSet()

            NewObjAmlo()

            If (ddlRrptType.SelectedValue = "M57") Then
                Dim qryAmlo = From tblAmlo In AMLO1059Entity.AMLO1059 Join tblBranch In AMLO1059Entity.SBRANCHes _
                      On tblAmlo.INIT_SOL_ID Equals tblBranch.SOL_ID _
                     Group Join tblTran In AMLO1059Entity.MTSCTYPE_1_05_9 On tblAmlo.KEY_FLG Equals tblTran.TSCTYPEID
                      Into leftJoinAmlo = Group
                      From tblLeftAmlo In leftJoinAmlo.DefaultIfEmpty
                     Order By tblAmlo.ID Ascending _
                Select tblAmlo.ID, tblAmlo.KEY_FLG, tblAmlo.P3_TRAN_ID, tblAmlo.P3_TRANS_TYPE, tblAmlo.P3_TRANS_DATE, tblAmlo.P3_TRAN_AMT, tblAmlo.INIT_SOL_ID, tblAmlo.P3_FORACID, tblAmlo.P3_CUST_NAME, tblAmlo.P2_CUST_FNAME, tblAmlo.P2_CUST_LNAME, _
                tblAmlo.P3_FORACID2, tblAmlo.P3_CUST_NAME2, tblAmlo.P2_CUST_FNAME2, tblAmlo.P2_CUST_LNAME2, TSCTYPEID = If(tblLeftAmlo.TSCTYPEID Is Nothing, "-", tblLeftAmlo.TSCTYPEID), _
                tblBranch.DEPTCODE, tblBranch.ZONECODE, tblBranch.BRANCHCODE, _
                tblLeftAmlo.TSCTYPENAME, tblAmlo.CREATEBY, tblAmlo.CREATEDATE, tblAmlo.UPDATEBY, tblAmlo.UPDATEDATE, _
                tblAmlo.ISCONFIRM, tblAmlo.ISSEND, tblAmlo.ISDELETE, tblAmlo.DCN_DOCUMENTNO

                If Not (ddlTransType.SelectedValue = "0") Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.KEY_FLG = ddlTransType.SelectedValue)
                End If

                If Not (ddlDepartment.SelectedValue = "0") Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DEPTCODE = ddlDepartment.SelectedValue)
                End If

                If Not (ddlBranch.SelectedIndex = 0) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.BRANCHCODE.Contains(ddlBranch.SelectedValue))
                End If

                'If Not (txtYear.Text = "") Then
                '    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.RPTYEAR.Value = CInt(txtYear.Text))
                'End If
                If Not (txtNo.Text = "") Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.DCN_DOCUMENTNO.Contains(txtNo.Text))
                End If

                If Not (rdoUserConfirm.SelectedValue = -1) Then
                    qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISCONFIRM = rdoUserConfirm.SelectedValue)
                End If

                If rdolTypeDate.SelectedValue = 1 Then
                    If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                        Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text) & " 00:00:00"
                        Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text) & " 23:59:59"
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.P3_TRANS_DATE >= strStartDate And tbl_Amlo.P3_TRANS_DATE <= strEndDate)
                    End If
                Else

                    If Not (txtDateStart.Text = "" And txtDateEnd.Text = "") Then
                        Dim strStartDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateStart.Text) & " 00:00:00"
                        Dim strEndDate As String = clsFormatDate.chkDateThOrEn(clsDateFormat.enmDateCultureInfo.En, txtDateEnd.Text) & " 23:59:59"
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.CREATEDATE >= strStartDate And tbl_Amlo.CREATEDATE <= strEndDate)
                    End If
                End If

                If Not (rdolIsSend.SelectedValue = 0) Then
                    If (rdolIsSend.SelectedValue = 1) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 1)
                    ElseIf (rdolIsSend.SelectedValue = 2) Then
                        qryAmlo = qryAmlo.Where(Function(tbl_Amlo) tbl_Amlo.ISSEND = 0)
                    End If

                End If

                dsAmloXML.Tables.Add(clsDataTable.LINQToDataTable(qryAmlo, "Amlo").Copy)
                If (qryAmlo.Count = 0) Then
                    lblMsg.Visible = True
                    Exit Sub
                Else
                    lblMsg.Visible = False
                End If

                Dim AmloXML As New clsAmloXML
                Dim path As String
                Dim iCount, iAllRec As Integer
                Dim CountChk As Integer = 0
                iAllRec = dsAmloXML.Tables("Amlo").Rows.Count

                For iCount = 0 To iAllRec - 1
                    Dim chkConfirmGen As CheckBox = CType(grdAmlo1059.Rows(iCount).FindControl("CheckBox1"), CheckBox)
                    If (chkConfirmGen.Checked) Then
                        CountChk += 1
                    End If
                Next
                '////////////////////////
                path = "C:\EXPORTXML\AMLO1059.xml"
                FileCopy("C:\EXPORTXML\Template\AMLO1059.xml", "C:\EXPORTXML\AMLO1059.xml")

                iAllRec = dsAmloXML.Tables("Amlo").Rows.Count

                Dim idList = New List(Of Integer)
                For iCount = 0 To iAllRec - 1

                    Dim chkConfirmGen As CheckBox = CType(grdAmlo1059.Rows(iCount).FindControl("CheckBox1"), CheckBox)
                    If (chkConfirmGen.Checked) Then
                        CountChk += 1

                        Dim hdfId = CType(grdAmlo1059.Rows(iCount).FindControl("hdfId"), HiddenField)
                        idList.Add(CInt(hdfId.Value))
                    End If
                Next

                If Not (AmloXML.AddAMLO1059node(path, idList, CountChk)) Then
                    Exit Sub
                End If

                DownloadFile("C:\ExportXML\AMLO1059.xml", "AMLO09" & Now.Date.ToString("yyyyMMdd") & ".xml", True)
                ClearObj()
            End If



        Catch ex As Exception
            ScriptManager.RegisterStartupScript(UpdatePanel1, _
                                                              UpdatePanel1.GetType(), _
                                                              "clientScript", _
                                                              "alert('" + ex.Message.Replace("'", "").Replace("""", "") + "')", _
                                                              True)
        End Try
    End Sub
End Class
